1

私はこの問題に一日中苦労しています。私は自分のプロジェクトでいくつかのライブラリを使用していますが、常にいくつかのコンパイル エラーが発生しました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後方に移動するとさらに悪化し、より多くのシンボルが未定義であると主張されます:

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
4

2 に答える 2

6

はい、ライブラリの順序は間違いなく重要です。最初にライブラリを使用するものを配置し、次にライブラリを配置する必要があります。

ライブラリ A がライブラリ B の何かに依存し、ライブラリ B がライブラリ A の何かを必要とする場合があったので、ライブラリ A をリストに 2 回入れる必要があります。

リンカが動作する方法は、オブジェクト ファイルを処理し、次にライブラリを読み取って、オブジェクト ファイルに存在しないシンボルを解決することです。ライブラリに依存関係を解決するためのオブジェクト「ファイル」がある場合、それらのパーツが含まれます。その後、次のライブラリに進みます。以前のライブラリで見たものを「記憶」していません。

于 2013-07-16T07:02:47.297 に答える
0

これはどうですか、liburldecode.a のすぐ後ろに liburlaes.a を置いてください。

 g++ -Wall -O2 -fPIC  -o encode encode.o  ./libs/liburldecode.a **./libs/liburlaes.a **./libs/libimageenc.a ./libs/libmbpicenc.a 
于 2013-07-16T07:50:23.487 に答える