關于c/c++互調的事情

c/c++互調其實是老生常談的事情了,不過還是說說自己的理解~。

c/c++互調無外乎c++文件里調用c的函數或者c文件調用c++的函數,涉及的原理也很簡單,函數symbol,眾所周知,c++支持重載,所以在我們看到的cpp源文件的函數,在編譯后,不是看到函數名,c++編譯后的函數symbol會攜帶更多信息,包括返回值、參數類型。c/c++互調出現問題在于編譯后的鏈接階段,一個個.c,.cpp文件經過編譯后,生成“可重定為目標文件”,在該目標文件中調用的所有外部函數(非本文件內的函數),都由一個個函數symbol表示,在鏈接階段會解決這些函數symbol的實際地址(so共享庫的過程將更復雜一些)。到這里我們就可以想象了,在c文件里調用的c++函數,如果不做任何處理,那鏈接這步的時候,就會用c的函數symbol方式去找函數的address,c++是文件也是同樣的情況,然后就看到了讓初學者頭疼不已的Undefine symbol了。

無圖無真相,還是看看實際的情況最有印象,寫一個最簡單的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就是函數symbol,倘若我把test.c改為test.cpp,gcc -c test.cpp,objdump -d test.o,看到就是c++形式的函數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

我們接著把問題發散一下,關于gcc編譯鏈接cpp文件的問題。

gcc可不可以編譯cpp文件呢?答案是可以的,而且通過剛才的例子,可以看到不同的文件后綴,相同的文件內容,反匯編出來的結果就不一樣,所以可以理解為gcc是通過文件后綴去認為c還是c++的,在這里要提一下,gcc可以編譯cpp文件,但不會自動鏈接c++的標準庫,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,則會出現如下錯誤:

/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()'

這個時候需要手動指明鏈接標準c++庫,

gcc test.cpp -lstdc++

回到最開始討論的問題,c/c++ 互調,無論是c文件調用c++還是相反的情況,都是把調用的函數symbol作為c的symbol,即如果c要用c++提供的函數,那就想辦法在cpp文件中把函數弄成c的symbol,怎么做呢?extern "C"即可。

extern "C"
{
    int testFunc(int a,int b);
}

int testFunc(int a,int b)
{
    return a+b;
}

同理可以通過objdump驗證函數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++

運行結果:

tsing@tsing-VirtualBox:~/myCode/wrapObj$ ./MyExe
create object
exec MyFunc
destroy object
tsing@tsing-VirtualBox:~/myCode/wrapObj$

完畢~。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容