c/c++互調(diào)其實(shí)是老生常談的事情了,不過還是說說自己的理解~。
c/c++互調(diào)無外乎c++文件里調(diào)用c的函數(shù)或者c文件調(diào)用c++的函數(shù),涉及的原理也很簡單,函數(shù)symbol,眾所周知,c++支持重載,所以在我們看到的cpp源文件的函數(shù),在編譯后,不是看到函數(shù)名,c++編譯后的函數(shù)symbol會攜帶更多信息,包括返回值、參數(shù)類型。c/c++互調(diào)出現(xiàn)問題在于編譯后的鏈接階段,一個個.c,.cpp文件經(jīng)過編譯后,生成“可重定為目標(biāo)文件”,在該目標(biāo)文件中調(diào)用的所有外部函數(shù)(非本文件內(nèi)的函數(shù)),都由一個個函數(shù)symbol表示,在鏈接階段會解決這些函數(shù)symbol的實(shí)際地址(so共享庫的過程將更復(fù)雜一些)。到這里我們就可以想象了,在c文件里調(diào)用的c++函數(shù),如果不做任何處理,那鏈接這步的時候,就會用c的函數(shù)symbol方式去找函數(shù)的address,c++是文件也是同樣的情況,然后就看到了讓初學(xué)者頭疼不已的Undefine symbol了。
無圖無真相,還是看看實(shí)際的情況最有印象,寫一個最簡單的test.c文件:
int testFunc(int a,int b)
{
int c = 0;
c = a + b;
return c;
}
我們用gcc -c test.c編譯該文件,然后通過objdump -d test.o,
Disassembly of section .text:
0000000000000000 <testFunc>:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: 89 7d ec mov %edi,-0x14(%rbp)
7: 89 75 e8 mov %esi,-0x18(%rbp)
a: 8b 55 ec mov -0x14(%rbp),%edx
d: 8b 45 e8 mov -0x18(%rbp),%eax
10: 01 d0 add %edx,%eax
12: 89 45 fc mov %eax,-0x4(%rbp)
15: 8b 45 fc mov -0x4(%rbp),%eax
18: 5d pop %rbp
19: c3 retq
testFunc就是函數(shù)symbol,倘若我把test.c改為test.cpp,gcc -c test.cpp,objdump -d test.o,看到就是c++形式的函數(shù)symbol, 即_Z8testFuncii,
isassembly of section .text:
0000000000000000 <_Z8testFuncii>:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: 89 7d ec mov %edi,-0x14(%rbp)
7: 89 75 e8 mov %esi,-0x18(%rbp)
a: 8b 55 ec mov -0x14(%rbp),%edx
d: 8b 45 e8 mov -0x18(%rbp),%eax
10: 01 d0 add %edx,%eax
12: 89 45 fc mov %eax,-0x4(%rbp)
15: 8b 45 fc mov -0x4(%rbp),%eax
18: 5d pop %rbp
19: c3 retq
我們接著把問題發(fā)散一下,關(guān)于gcc編譯鏈接cpp文件的問題。
gcc可不可以編譯cpp文件呢?答案是可以的,而且通過剛才的例子,可以看到不同的文件后綴,相同的文件內(nèi)容,反匯編出來的結(jié)果就不一樣,所以可以理解為gcc是通過文件后綴去認(rèn)為c還是c++的,在這里要提一下,gcc可以編譯cpp文件,但不會自動鏈接c++的標(biāo)準(zhǔn)庫,g++則會自動鏈接。還是以剛才的代碼舉例:
#include <iostream>
using namespace std;
int testFunc(int a,int b)
{
return (a+b);
}
int main()
{
int c = testFunc(1,2);
cout<< c <<endl;
return 0;
}
文件命名為test.cpp,直接用g++ test.cpp,會生成a.out。但如果用gcc test.cpp,則會出現(xiàn)如下錯誤:
/tmp/ccQlLuwK.o: In function `main':
test.cpp:(.text+0x3a): undefined reference to `std::cout'
test.cpp:(.text+0x3f): undefined reference to `std::ostream::operator<<(int)'
test.cpp:(.text+0x44): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cpp:(.text+0x4c): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccQlLuwK.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x7a): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x89): undefined reference to `std::ios_base::Init::~Init()'
這個時候需要手動指明鏈接標(biāo)準(zhǔn)c++庫,
gcc test.cpp -lstdc++
回到最開始討論的問題,c/c++ 互調(diào),無論是c文件調(diào)用c++還是相反的情況,都是把調(diào)用的函數(shù)symbol作為c的symbol,即如果c要用c++提供的函數(shù),那就想辦法在cpp文件中把函數(shù)弄成c的symbol,怎么做呢?extern "C"即可。
extern "C"
{
int testFunc(int a,int b);
}
int testFunc(int a,int b)
{
return a+b;
}
同理可以通過objdump驗(yàn)證函數(shù)symbol。
在想一下,如果c文件里要使用c++的類要怎么做呢?我想到方法多少還是可以做的。先寫一個簡單的類,
MyClass.h
class MyClass
{
public:
MyClass();
~MyClass();
void MyFunc();
};
Wrap.h
typedef void* ObjHandle;
#ifdef __cplusplus
extern "C"
{
#endif
ObjHandle CreateObj();
void DestroyObj(ObjHandle handle);
void WrapFunc(ObjHandle handle);
#ifdef __cplusplus
}
#endif
MyClass.cpp
#include "MyClass.h"
#include "Wrap.h"
#include <iostream>
using namespace std;
MyClass::MyClass()
{
cout<<"create object"<<endl;
}
MyClass::~MyClass()
{
cout<<"destroy object"<<endl;
}
void MyClass::MyFunc()
{
cout<<"exec MyFunc"<<endl;
}
ObjHandle CreateObj()
{
MyClass * p = new MyClass();
return (ObjHandle)p;
}
void DestroyObj(ObjHandle handle)
{
if(handle)
{
MyClass * p = (MyClass*)handle;
delete p;
}
}
void WrapFunc(ObjHandle handle)
{
if(handle)
{
MyClass * p = (MyClass*)handle;
p->MyFunc();
}
}
main.c
#include <stdio.h>
#include "Wrap.h"
int main()
{
ObjHandle handle = NULL;
handle = CreateObj();
WrapFunc(handle);
DestroyObj(handle);
return 0;
}
編譯鏈接
gcc -o MyExe MyClass.cpp main.c -lstdc++
運(yùn)行結(jié)果:
tsing@tsing-VirtualBox:~/myCode/wrapObj$ ./MyExe
create object
exec MyFunc
destroy object
tsing@tsing-VirtualBox:~/myCode/wrapObj$
完畢~。