Reputation: 1759
First of all I am trying to use
#include <openssl/evp.h>
I am getting the common linker error:
undefined reference to `EVP_CIPHER_CTX_new'
which I know can be solved by using -lcrypto
when compiling. I am able to do this when compiling manually i.e
g++ encrv2.cpp -o encrv2 -lcrypto
though I am trying to create a Makefile and putting my encryption/decryption functions in a seperate header file (AesGcm) and am unable to get around the linker issue. Below is my Makefile so far and the output when I try and make all.
CXX = g++
CXXFLAGS = -std=c++14
LDFLAGS= -lcrypto
all: encrv2
AesGcm.o: AesGcm.cpp AesGcm.h
$(CXX) $(CXXFLAGS) -c AesGcm.cpp $(LDFLAGS)
encrv2.o: encrv2.cpp AesGcm.h
$(CXX) $(CXXFLAGS) -c encrv2.cpp $(LDFLAGS)
encrv2: encrv2.o AesGcm.o
clean:
rm -f *~ *.o encrv2
Giving the following error:
cc -lcrypto encrv2.o AesGcm.o -o encrv2
AesGcm.o: In function `AesGcm::encrypt(unsigned char*, int, unsigned char*, int, unsigned char*, unsigned char*, unsigned char*, unsigned char*)':
AesGcm.cpp:(.text+0x3e): undefined reference to `EVP_CIPHER_CTX_new'
AesGcm.cpp:(.text+0x58): undefined reference to `EVP_aes_256_gcm'
AesGcm.cpp:(.text+0x77): undefined reference to `EVP_EncryptInit_ex'
AesGcm.cpp:(.text+0xae): undefined reference to `EVP_EncryptInit_ex'
AesGcm.cpp:(.text+0xe0): undefined reference to `EVP_EncryptUpdate'
AesGcm.cpp:(.text+0x111): undefined reference to `EVP_EncryptUpdate'
AesGcm.cpp:(.text+0x148): undefined reference to `EVP_EncryptFinal_ex'
AesGcm.cpp:(.text+0x17b): undefined reference to `EVP_CIPHER_CTX_ctrl'
AesGcm.cpp:(.text+0x197): undefined reference to `EVP_CIPHER_CTX_free'
AesGcm.o: In function `AesGcm::decrypt(unsigned char*, int, unsigned char*, int, unsigned char*, unsigned char*, unsigned char*, unsigned char*)':
AesGcm.cpp:(.text+0x30a): undefined reference to `EVP_CIPHER_CTX_new'
AesGcm.cpp:(.text+0x324): undefined reference to `EVP_aes_256_gcm'
AesGcm.cpp:(.text+0x343): undefined reference to `EVP_DecryptInit_ex'
AesGcm.cpp:(.text+0x37a): undefined reference to `EVP_DecryptInit_ex'
AesGcm.cpp:(.text+0x3ac): undefined reference to `EVP_DecryptUpdate'
AesGcm.cpp:(.text+0x3dd): undefined reference to `EVP_DecryptUpdate'
AesGcm.cpp:(.text+0x410): undefined reference to `EVP_CIPHER_CTX_ctrl'
AesGcm.cpp:(.text+0x441): undefined reference to `EVP_DecryptFinal_ex'
AesGcm.cpp:(.text+0x450): undefined reference to `EVP_CIPHER_CTX_free'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'encrv2' failed
make: *** [encrv2] Error 1
Upvotes: 0
Views: 1813
Reputation: 101041
You're using the wrong variable.
LDFLAGS
are intended to be used for linker flags such as -L
, etc. that need to come "earlier" in the link line. That variable is not intended to have libraries that are added to the link line.
You should be using the LDLIBS
variable for that:
LDLIBS = -lcrypto
The default rule for linking, which is what you're using since you don't write one yourself, looks something like this:
%: %.o
# recipe to execute (built-in):
$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@
Note that LDLIBS
comes after your object files, which is what you need to avoid this linker error. Order of object files versus libraries, and between libraries themselves, is very important to the linker.
Upvotes: 2