关于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$

完毕~。

链接:https://www.jianshu.com/p/8483404f14ba

猜你喜欢

转载自blog.csdn.net/weixin_42323413/article/details/84297893