C语言里extern "C" 是什么意思?

电子说

1.2w人已加入

描述

经常在C语言的头文件中看到下面的代码:

#ifdef __cplusplus extern "C" { #endif // all of your legacy C code here #ifdef __cplusplus } #endif

这通常用于C++和C混合编程的时候,为了防止C++的编译器在编译C文件的时候出现错误;众所周知,C++可以进行函数名重载,但是C则没有这种功能,那这和extern "C"又有什么关系呢?
先看下面这个表格,如下所示;

未添加 extern "C"

test.h

#ifndef TEST_H #define TEST_H void foo1(void); void foo2(void); void foo3(int i); #endif

test.c

void foo1(void){} void foo2(void) {} void foo3(int i){} int main(int argc,char** argv){    foo1();  foo2();  foo3(1);   return 0; }

编译这两个文件,生成test.o文件,通过objdump查看函数符号;

g++ -c test.c test.h objdump -t test.o

可以看到函数符号已经被编译器修改了;

C

添加extern "C"

test.h

#ifndef TEST_H #define TEST_H #ifdef __cplusplus extern "C" { #endif void foo1(void); void foo2(void); void foo3(int i); #ifdef __cplusplus } #endif #endif

test.c

#ifdef __cplusplus extern "C" { #endif void foo1(void){} void foo2(void) {} void foo3(int i){} #ifdef __cplusplus } #endif int main(int argc,char** argv){    foo1();  foo2();  foo3(1);   return 0; }

编译这两个文件,生成test.o文件,通过objdump查看函数符号;

g++ -c test.c test.h objdump -t test.o

这时候函数符号是正确的;

C

extern "C" 是告诉C++的编译器不要打我这些C函数的主意。

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 相关推荐
  • C

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分