Andy Andy - 1 year ago 129
C Question

Linking libcrypto in GCC

Okay, I'm pretty sure I'm doing something immensely stupid, but I can't seem to figure it out, so here it goes.

I'm attempting to use OpenSSLs EVP crap to do some encryption. I'm pretty sure my code is right, but I can't seem to get it to compile. I'm using gcc, and ubuntu 32bit precise with libssl-dev installed and at the latest version.

The project currently consists of one file,


#include <openssl/evp.h>
i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1() ... );

among other various calls.

Here is how I invoke gcc:

gcc -Wall -g -lssl -lcrypto -o program program.c

Then I get output like this

/home/andy/program/program.c:31: undefined reference to `EVP_sha1'
/home/andy/program/program.c:31: undefined reference to `EVP_aes_256_cbc'
/home/andy/program/program.c:31: undefined reference to `EVP_BytesToKey'
/home/andy/program/program.c:44: undefined reference to `EVP_CIPHER_CTX_init'

So the include is clearly working:

andy@ProgStation2:/usr/include$ find . | grep evp.h

Here is the output of
locate libcrypto
. My best guess is that this is a stupid location for it and is why my link is failing, so I tried
with no luck as well.


I'm kind of stumped. If anyone wants to make me feel like a fool, I'd be very excited to figure out what i'm doing wrong!

Answer Source

It turns out it was something stupid. In the linker step, I was using gcc -Wall -g -lssl -lcrypto -o program program.o. I needed to move the library links to after the object file I was linking, and put libssl before libcrypto:

gcc -Wall -g -o program program.o -lssl -lcrypto
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download