Why Do I Get "Undefined Reference" Errors When Linking Against Openssl?

Why Do I Get "Undefined Reference" Errors When Linking Against Openssl?

My compilation process throws around errors like

..undefined reference to `BN_cmp'

although I include <openssl/bn.h>

and run gcc -lssl -lcrypto test.c -o test

can someone help? (openssl libssl1.0.0, libssl-dev are installed)

1 Answer

The solution is as simple as adding the -l flags at the end:

gcc test.c -o test -lssl -lcrypto

The order matters because ld since Ubuntu 11.04 is invoked with the -as-needed switch by default, so that files/libraries which depend on other libraries must come before these other libraries, i.e. test.c needs libcrypto, so it must come before -lcrypto.

For more information, see Toolchain Transition in Natty Narwhal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.