Undefined reference in app which includes library
-
As the title suggests, I'm dealing with a odd issue, where I have a library which includes OpenSSL, and an app which uses the library. The library builds without problems.
The problem comes when I include the library in my application, I get an undefined reference error in my app whenever I use any OpenSSL functions in my library.
This is my library's ".pro" file
# ... # Some lines were removed for brevity # ... # Default rules for deployment. unix { target.path = $$[QT_INSTALL_PLUGINS]/generic } !isEmpty(target.path): INSTALLS += target # Add OpenSSL lib LIBS += -lcrypto
This the ".pro" file for my app.
# ... # Some lines were removed for brevity # ... # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target unix:!macx: LIBS += [libs path]/Crypto/Build/Release/ -lCrypto INCLUDEPATH += [libs path]/Crypto/Build/Release DEPENDPATH += [libs path]/Crypto/Build/Release unix:!macx: PRE_TARGETDEPS += [libs path]/Crypto/Build/Release/libCrypto.a
Couple of notes:
Some of the errors say that it's missing the source file(?), and the object file(?) ,even though it's only a ".a" file, and a header.If I move the definition of one of the functions (Which use OpenSSL) to the declaration, the app still specifies that it's an undefined reference for the OpenSSL function.
Removing the library include lines from the app's ".pro" file doesn't do anything.
This only happens with functions that use OpenSSL (Or probably any non system libraries).
I don't have much experience building libraries for Linux, so I might be missing something obvious. Any help is appreciated.
-
@Christian-Ehrlicher said in Undefined reference in app which includes library:
ERR_get_error is in libssl.
All these are in libssl. But while you link against that in your first case (
-lssl
) you do not do so in your second case, you only go-lCrypto
. hence the undefined references!I think you are thinking that once you have created your
libCrypto.a
it contains thecrypto
/ssl
you specified while building it. It does not!. The-lcrypto -lssl
you specified tells it these are external libraries it can use. You must re-specify them whenever you link against your-lCrypto
. (And the corresponding.so
files must be findable at runtime.) Follow now?BTW, I find it incredibly confusing that you are building a
libCrypto.a
linking with alibcrypto.a
(Linux case-sensitive distinguishes). Please name your library differently from the libraries it links against! -
Your app probably needs to link to OpenSSL, too.
-
Why? Isn't the point of using libraries to not have to include multiples of the same library?
Also, I tried that, it didn't work.
However, here's the really weird part, if I call the same (OpenSSL) function the compiler is telling me is undefined, the app builds successfully. I genuinely have no idea what's happening -
What exact linker error do you get? Please post the error output. If you use functions from a library you have to link against it.
-
@Christian-Ehrlicher Sorry for the late reply, I'm off work atm.
This is taken directly from the output I'm getting from QT creator
vBact::Test(int)
Is a function I have defined in my library, in its definition I callERR_load_crypto_strings
function from OpenSSL. That's where the linker(?) tells me there's a problem. Here's the weird part, if I call the same (OpenSSL) function from my app it builds fine.If you use functions from a library you have to link against it.
How can I be sure I'm linking correctly? I just used QT's library wizard to add my library to my app.
-
ERR_load_crypto_strings is a (deprecated) function from libcrypto so you must link against the crypto lib
How can I be sure I'm linking correctly?
By reading the docs of the function you use and hope that you find a notice on which library the function is in.
-
ERR_load_crypto_strings is a (deprecated) function from libcrypto so you must link against the crypto lib
Maybe the terminology is lost on me, but when you say linking you mean "including" the library in the ".pro" file, right?
If so that's what I'm doing here, no?# Add OpenSSL lib LIBS += -lcrypto
The problem is not
ERR_load_crypto_strings
itself, it happens with every function from OpenSSL.For example, I've uncocumented some code I commented for testing purposes, it uses even more OpenSSL functions, this is the output I'm getting
-
ERR_get_error is in libssl.
-
But I include libcrypto in my library, there it builds fine, even without libssl.
The problem comes when compiling my app with my library. I can copy the code from my library into my app, and compile with libcrypto, and it'll work fine.Why doesn't it work when I build with my library?
-
Update, maybe?
I've been trying to manually build the library with
g++ -c Crypto.cpp -lcrypto -lssl ar r libCrypto.a Crypto.o
This works, it creates a
libCrypto.a
file without problems.Next, I created a small test app with only a single
main
function, which uses the functionvBact::Encrypt()
from my library. To build it I'm usingg++ main.cpp -L./ -lCrypto
This produces the following output
.//libCrypto.a(Crypto.o): In function `vBact::HandleSSL(int)': Crypto.cpp:(.text+0x36): undefined reference to `ERR_load_crypto_strings' Crypto.cpp:(.text+0x42): undefined reference to `ERR_get_error' Crypto.cpp:(.text+0x55): undefined reference to `ERR_error_string' .//libCrypto.a(Crypto.o): In function `vBact::Encrypt(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int)': Crypto.cpp:(.text+0x122): undefined reference to `EVP_CIPHER_CTX_free' Crypto.cpp:(.text+0x127): undefined reference to `EVP_CIPHER_CTX_new' Crypto.cpp:(.text+0x2a6): undefined reference to `EVP_aes_256_cbc' Crypto.cpp:(.text+0x2d1): undefined reference to `EVP_EncryptInit_ex' Crypto.cpp:(.text+0x395): undefined reference to `EVP_EncryptUpdate' Crypto.cpp:(.text+0x3eb): undefined reference to `EVP_EncryptFinal_ex' .//libCrypto.a(Crypto.o): In function `vBact::Test(int)': Crypto.cpp:(.text+0x52a): undefined reference to `ERR_load_crypto_strings' collect2: error: ld returned 1 exit status
Where am I going wrong?
-
@Christian-Ehrlicher said in Undefined reference in app which includes library:
ERR_get_error is in libssl.
All these are in libssl. But while you link against that in your first case (
-lssl
) you do not do so in your second case, you only go-lCrypto
. hence the undefined references!I think you are thinking that once you have created your
libCrypto.a
it contains thecrypto
/ssl
you specified while building it. It does not!. The-lcrypto -lssl
you specified tells it these are external libraries it can use. You must re-specify them whenever you link against your-lCrypto
. (And the corresponding.so
files must be findable at runtime.) Follow now?BTW, I find it incredibly confusing that you are building a
libCrypto.a
linking with alibcrypto.a
(Linux case-sensitive distinguishes). Please name your library differently from the libraries it links against! -
@JonB
I changedg++ main.cpp -L./ -lCrypto
tog++ main.cpp -L./-lMyCrypto -lssl -lcrypto
And it worked? But this doesn't really make sense to me.
We're using other libraries, which I'm sure use other libraries, and yet we don't have to go through the entire dependency chain every time we want to build a project, why is this the case here? -
@Josef-Lintz
There seems to be no peace for the wicked :) I tell you how to make what you showed work, and now you ask me why something else of which I have no knowledge does/does not work!Maybe the other cases are static libraries which do actually include other libraries? Maybe the
ar
commands include the other libraries too? Maybe there is an option to theld
linker where you tell it to mark the.a
file being generated with references to the other libraries it will need?You could use e.g.
nm
to examine the symbols etc. in each library file to understand what is there and how that differs between your cases. -
So I figured out my problem. I incorrectly assumed that static libraries in Linux behave similarly to ".lib" files in Windows. According to this answer, you cannot "embed" dependencies in static libraries. So I recompiled my library as a shared object and now it works.
g++ -c -fPIC Crypto.cpp g++ -shared Crypto.o -o libMyCrypto.so -lcrypto
But there's one last thing that bugs me. If I manually create a shared object and link it with my app, and rename my library to something else, I can't run my app anymore because the app can't find its dependency.
If I build the app using QT creator, there's no ".so" file in the
build
directory, and it runs fine.Does QT embed ".so" files in the executable?
-
@Josef-Lintz said in Undefined reference in app which includes library:
and rename my library to something else, I can't run my app anymore because the app can't find its dependency.
Correct.
If I build the app using QT creator, there's no ".so" file in the build directory, and it runs fine.
Then it finds it somewhere else, perhaps in your project or whatever. There is
LD_LIBRARY_PATH
environment variable, that might be relevant.Does QT embed ".so" files in the executable?
No. And Qt does not "do anything" anyway. Creator is just building using
make
etc., anything to do with that is external to Qt.