# Using -fPIC flag to complied with position indpendent code. $ g++ -c -fPIC ./shared.cpp -o ./shared.o
使用nm指令查看符号表,指令与输出如下:
1 2 3 4 5 6 7 8 9 10 11
$ nm ./shared.o
U _GLOBAL_OFFSET_TABLE_ 000000000000007d T _Z10helloworldv 0000000000000032 T _ZN8external10helloworldEi 0000000000000000 T _ZN8external10helloworldEv U _ZNSolsEPFRSoS_E U _ZNSolsEi U _ZSt21ios_base_library_initv U _ZSt4cout ...
//File name: shared.h //header file. without function implementation. #pragma once extern"C"{ namespace external { voidhelloworld(); inthelloworld(int a); } voidhelloworld();
}
编译上述代码我们会得到错误:
1 2 3 4 5 6
$ g++ -C -fPIC ./shared.cpp -o ./shared.o
In file included from ./shared.cpp:2: ./shared.h:6:13: error: conflicting declaration of C function ‘int external::helloworld(int)’ 6 | int helloworld(int a);
//File name: shared.h //header file. without function implementation. #pragma once extern"C"{ namespace external { voidhelloworld(); } }
使用nm指令查看其符号表:
1 2 3 4 5 6
$ nm ./shared.o
... U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_ U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc 0000000000000000 T helloworld