1

如果我做

>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

为什么?

根据 Alec Teal 的要求,SSCCE:

> 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
4

0 回答 0