Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

gcc - Errors that refer to a bunch of unresolved OpenSSL symbols that clearly exist?

I am building a shared library (we'll call it "foo") that makes use of another library (we'll call it "bar"). "bar" makes use of some functions from OpenSSL.

Here's where the problem surfaces.

"bar" was compiled as a static library and it would appear that OpenSSL was too. So when I link the library ("foo"), I include the:

  • object files for "foo"
  • static library libbar.a
  • OpenSSL static libraries libcrypto.a and libssl.a

The build command looks something like this:

g++ -Wl,-soname,libfoo.so -shared file1.o file2.o libbar.a 
  libcrypto.a libssl.a -o libfoo.so

However, I get a ton of errors:

ld: ./obj/libbar.a(file1.c.o): in function initialize_openssl:
  ssl.c:117: error: undefined reference to 'SSL_library_init'

Running the following command:

nm libssl.a | grep SSL_library_init

Produces the following output:

00000000 T SSL_library_init

So obviously there is nothing wrong with the OpenSSL libraries. What could have possibly caused something like this? Here are the three commands used to build OpenSSL:

export cross=arm-linux-androideabi-
./Configure android --prefix=~/openssl-arm
make CC="${cross}gcc" AR="${cross}ar r" RANLIB="${cross}ranlib"

The compilation process completed without any errors, so I'm utterly baffled.

Why am I getting linker errors that refer to a bunch of OpenSSL symbols that clearly exist?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem was caused by the order of the libraries in the link command. Switching the order of libcrypto.a and libssl.a resolved all of the symbols.

GCC uses LD by default, and its a single pass linker. When you have two libraries, like libssl and libcrypto linked in a particular order, it means libssl depends on symbols from libcrypto. Therefore, libssl must precede libcrypto (or libcrypto must follow libssl). It should be no surprise libssl relies upon libcrypto since libcrypto provides the crypto used by libssl.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...