假设有个文件如下:
$ cat test.c
int foo(int a)
{
return 1;
}
编译如下:
$ g++ test.c -c
$ nm test.o
00000000 T _Z3fooi
可见, c++ 编译器对名字做了修饰。
再编译如下:
$ gcc -c test.c
$ nm test.o
00000000 T foo
可见, c编译器未对函数名字修饰
现在修改代码如下:
$ cat test.c
extern "C" int foo(int a);
int foo(int a)
{
return 1;
}
编译如下:
$ g++ test.c -c
$ nm test.o
00000000 T foo
可见,此时 g++ 编译器也不对函数名字修饰了。
这是C和 C++代码混合使用的时候,常用的一种方法。