c++ - 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
解决方案
您使用了错误的变量。
LDFLAGS
旨在用于-L
需要在链接行中“更早”出现的链接器标志,例如 等。该变量不打算将库添加到链接行。
您应该LDLIBS
为此使用变量:
LDLIBS = -lcrypto
链接的默认规则,这是您正在使用的,因为您不自己编写一个,看起来像这样:
%: %.o
# recipe to execute (built-in):
$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@
请注意,LDLIBS
它位于您的目标文件之后,这是避免此链接器错误所需要的。目标文件与库的顺序以及库本身之间的顺序对链接器非常重要。
推荐阅读
- ios - 有没有办法防止工具栏出现在路线更改上?IOS IPAD
- ios - 地理服务崩溃
- rabbitmq - 为什么这个 ProcessWindowFunction 总是计算每个传入元素而不是一个窗口的所有元素?
- javascript - 许多 try-catch 语句在相当大的客户端 JavaScript 单页应用程序中的影响
- php - SQLite & PHP 准备好的语句
- html - 为什么我在 html 标签上收到 Uncaught SyntaxError '<'?
- android - android小部件TextClock淡入淡出动画
- android - 单击空白区域会触发要选择的布局(包含 Gif)
- sql - SQL Server - 用句点在行上拆分金额
- c# - 如何访问作为我知道的变量的父类的类?