关于LD_PRELOAD和extern "C"

在C++编写的源文件中,经常可以看到extern “C”,它的作用,在stackoverflow上有比较好的描述

extern "C" makes a function-name in C++ have 'C' linkage (compiler does not mangle the name) so that client C code can link to (i.e use) your function using a 'C' compatible header file that contains just the declaration of your function. Your function definition is contained in a binary format (that was compiled by your C++ compiler) that the client 'C' linker will then link to using the 'C' name.

Since C++ has overloading of function names and C does not, the C++ compiler cannot just use the function name as a unique id to link to, so it mangles the name by adding information about the arguments. A C compiler does not need to mangle the name since you can not overload function names in C. When you state that a function has extern "C" linkage in C++, the C++ compiler does not add argument/parameter type information to the name used for linkage.

Just so you know, you can specify "C" linkage to each individual declaration/definition explicitly or use a block to group a sequence of declarations/definitions to have a certain linkage:

extern "C" void foo(int);
extern "C"
{
   void g(char);
   int i;
}
If you care about the technicalities, they are listed in section 7.5 of the C++03 standard, here is a brief summary (with emphasis on extern "C"):

extern "C" is a linkage-specification
Every compiler is required to provide "C" linkage
a linkage specification shall occur only in namespace scope
all function types, function names and variable names have a language linkage
two function types with distinct language linkages are distinct types even if otherwise identical
linkage specs nest, inner one determines the final linkage
extern "C" is ignored for class members
at most one function with a particular name can have "C" linkage (regardless of namespace)
extern "C" forces a function to have external linkage (cannot make it static)
Linkage from C++ to objects defined in other languages and to objects defined in C++ from other languages is implementation-defined and language-dependent. Only where the object layout strategies of two language implementations are similar enough can such linkage be achieved


C++为了实现重载机制,编译时,会在函数名上加上参数类型等信息,由此区分开同名但是参数不同的重载函数,这种方法叫做mangle name,但是这样造成了C++中直接引用C语言库函数,出现undefine reference的错误,究其原因,就是编译器把C库函数名字加上了其他信息,不能和C库链接。为了使用大量现成的C语言库,C++中提供了extern “C”机制,用extern “C”声明过的函数,不会对其mangle,解决了名字匹配的问题。


下面是一个小例子

三个文件jia.h  jia.c  test.cpp 。test.cpp中调用jia.c中的函数,需要注意的是编译的方法!!!

//jia.h
int jia(int a,int b);

//jia.c
int jia(int a,int b)
{
      return a+b;
}

//test.cpp
#include<iostream>

using namespace std;

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

int main()
{
       cout<<jia(1,1)<<endl;
}

C源文件和C++源文件需要分开编译,然后链接

gcc -c jia.c
g++ -c test.cpp
g++ test.o jia.o -o test


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值