繁体   English   中英

g ++静态库的顺序重要吗?

[英]g++ the order of static library matters?

我整天都在为这个问题而苦苦挣扎。 我在我的项目中使用了一些库,并且总是抱怨抱怨那些undefined reference导致一些编译错误。

相关文件如下:

encode.cc//the main file
url_codec.h//the header of the libraries, got some function definition in it
libimageenc.a
libmbpicenc.a
liburlaes.a
liburldecode.a

生成文件类似于:

cflags = -Wall -O2 -fPIC

libpath=./libs/
libs+=$(libpath)liburlaes.a
libs+=$(libpath)liburldecode.a $(libpath)libimageenc.a $(libpath)libmbpicenc.a

cxx  = g++
bin  = encode

all: $(bin)

srcs = $(shell ls *.cc *.cpp)
objs = $(srcs:%.cc=%.o)

$(bin):${objs}
    $(cxx) $(cflags) $(inc) -o $@ ${objs} ${libs}

$(objs):%.o:%.cc
    $(cxx) $(cflags) $(inc) -c -o $@ $<

clean:
    rm -f *.o
    rm -f *.bak
    rm -f $(bin)

g ++编译器抱怨:

g++ -Wall -O2 -fPIC  -o encode encode.o ./libs/liburlaes.a ./libs/liburldecode.a ./libs/libimageenc.a ./libs/libmbpicenc.a
./libs/liburldecode.a(url_codec.o): In function `url_codec::offpic_url_decode_func(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int&)':
url_codec.cc:(.text+0x18da): undefined reference to `uuid_unparse'
./libs/liburldecode.a(crypto_aes.o): In function `encode_aes':
crypto_aes.cc:(.text+0x4e): undefined reference to `AES_cbc_encrypt'
./libs/liburldecode.a(crypto_aes.o): In function `decode_aes':
crypto_aes.cc:(.text+0xae): undefined reference to `AES_cbc_encrypt'
./libs/liburldecode.a(crypto_aes.o): In function `init_aes_encrypt_key':
crypto_aes.cc:(.text+0xf3): undefined reference to `AES_set_encrypt_key'
./libs/liburldecode.a(crypto_aes.o): In function `init_aes_decrypt_key':
crypto_aes.cc:(.text+0x143): undefined reference to `AES_set_decrypt_key'
collect2: ld returned 1 exit status

但是所有这些功能在liburlaes.a中都有很好的定义,如nm -C所示

    nm -C liburlaes.a | grep -i 'aes'
    decode_byaes.o:
         U AES_cbc_encrypt
         U AES_set_decrypt_key
         U AES_set_encrypt_key
00000060 T decode_byaes
00000000 T encode_byaes
00000110 T init_byaes_decrypt_key
000000c0 T init_byaes_encrypt_key
         U decode_byaes
         U encode_byaes
         U init_byaes_decrypt_key
         U init_byaes_encrypt_key

通过将liburlaes.a移至libs的末尾,事情不会变得更好,输出与上面完全相同。 libimageenc后移动libimageenc将使情况变得更糟,声称有更多未定义的符号:

libs+=$(libpath)liburldecode.a $(libpath)libimageenc.a $(libpath)libmbpicenc.a
libs+=$(libpath)liburlaes.a

那么,我该如何解决呢?

更新

我试图将liburlaes.a放在两面,但是它不起作用,我在libaray上加上“ **”来强调:

g++ -Wall -O2 -fPIC  -o encode encode.o **./libs/liburlaes.a** ./libs/liburldecode.a ./libs/libimageenc.a ./libs/libmbpicenc.a **./libs/liburlaes.a**
./libs/liburldecode.a(url_codec.o): In function `url_codec::offpic_url_decode_func(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int&)':
url_codec.cc:(.text+0x18da): undefined reference to `uuid_unparse'
./libs/liburldecode.a(crypto_aes.o): In function `encode_aes':
crypto_aes.cc:(.text+0x4e): undefined reference to `AES_cbc_encrypt'
./libs/liburldecode.a(crypto_aes.o): In function `decode_aes':
crypto_aes.cc:(.text+0xae): undefined reference to `AES_cbc_encrypt'
./libs/liburldecode.a(crypto_aes.o): In function `init_aes_encrypt_key':
crypto_aes.cc:(.text+0xf3): undefined reference to `AES_set_encrypt_key'
./libs/liburldecode.a(crypto_aes.o): In function `init_aes_decrypt_key':
crypto_aes.cc:(.text+0x143): undefined reference to `AES_set_decrypt_key'
collect2: ld returned 1 exit status
make: *** [encode] Error 1

是的,库的顺序绝对重要。 您需要先放置使用库的任何内容,然后放置库。

在某些情况下,库A依赖库B中的某些内容,而库B需要库A中的某些内容,因此您需要将库A放入列表两次。

链接器的工作方式是:处理目标文件,然后读取库以解析目标文件中不存在的符号。 如果库中有对象“文件”来解决依赖关系,则包括这些部分。 然后继续到下一个库。 它没有“记住”以前的库中看到的内容。

怎么样,只需将liburlaes.a放在liburldecode.a之后。

 g++ -Wall -O2 -fPIC  -o encode encode.o  ./libs/liburldecode.a **./libs/liburlaes.a **./libs/libimageenc.a ./libs/libmbpicenc.a 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM