首页 > 解决方案 > How to fix makefile to properly include -lcrypto to avoid linker error: undefined reference to `EVP_CIPHER_CTX_new'

问题描述

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

标签: c++makefileopenssl

解决方案


您使用了错误的变量。

LDFLAGS旨在用于-L需要在链接行中“更早”出现的链接器标志,例如 等。该变量不打算将库添加到链接行。

您应该LDLIBS为此使用变量:

LDLIBS = -lcrypto

链接的默认规则,这是您正在使用的,因为您不自己编写一个,看起来像这样:

%: %.o
#  recipe to execute (built-in):
        $(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@

请注意,LDLIBS它位于您的目标文件之后,这是避免此链接器错误所需要的。目标文件与库的顺序以及库本身之间的顺序对链接器非常重要。


推荐阅读