如果我这样做
>ar rc foobar.a foobar.o
>g++ -shared -fPIC foobar.a -o foobar.so
与
相比,生成的foobar.so
要小得多且缺少符号
>g++ -shared -fPIC foobar.o -o foobar.so
其中foobar.so
包含foobar.o
为什么?
SSCCE按照Alec Teal的要求:
> cat foobar.cpp
#include "foobar.h"
Foobar::Foobar() {}
> cat foobar.h
class Foobar {
public:
Foobar();
};
> g++ -fPIC -c foobar.cpp -o foobar.o
> ar rc foobar.a foobar.o
> nm foobar.a | grep Foobar
0000000000000000 T _ZN6FoobarC1Ev
0000000000000000 T _ZN6FoobarC2Ev
> g++ -fPIC -shared foobar.a -o foobar.so
> nm foobar.so | grep Foobar
> g++ -fPIC -shared foobar.o -o foobar.so
> nm foobar.so | grep Foobar
0000000000000680 T _ZN6FoobarC1Ev
0000000000000680 T _ZN6FoobarC2Ev
--whole-archive
选项,对我不起作用:
> g++ -fPIC -shared -Wl,--whole-archive foobar.a -o foobar.so
/usr/lib64/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0xd): undefined reference to `__init_array_end'
/usr/bin/ld: /usr/lib64/libc_nonshared.a(elf-init.oS): relocation R_X86_64_PC32 against undefined hidden symbol `__init_array_end' can not be used when making a shared object
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status