为什么“g ++ -shared foobar.a”失去了符号?

时间:2013-10-09 04:04:48

标签: g++

如果我这样做

>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
tristan的回答指出使用--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

0 个答案:

没有答案