在 GCC 中链接 libssl 和 libcrypto [英] Linking libssl and libcrypto in GCC
问题描述
我正在尝试使用 OpenSSL 的 EVP 接口进行一些加密.我很确定我的代码是正确的,但我似乎无法编译它.我正在使用 GCC 和 Ubuntu 32 位精确,安装了 libssl-dev 并且是最新版本.
I'm attempting to use OpenSSL's EVP interface 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 32-bit precise with libssl-dev installed and at the latest version.
该项目当前由一个文件组成,program.c
.
The project currently consists of one file, program.c
.
#include <openssl/evp.h>
...
i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1() ... );
...
EVP_CIPHER_CTX_init(e_ctx);
在其他各种电话中.
这是我调用 gcc 的方式:
Here is how I invoke gcc:
gcc -Wall -g -lssl -lcrypto -o program program.c
然后我得到这样的输出
/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'
所以包含显然有效:
andy@ProgStation2:/usr/include$ find . | grep evp.h
./openssl/evp.h
这是locate libcrypto
的输出.我最好的猜测是这对它来说是一个愚蠢的位置,这就是我的链接失败的原因,所以我在 -lcrypto
之前尝试了 -L/usr/lib/i386-linux-gnu
代码>也没有运气.
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 -L/usr/lib/i386-linux-gnu
before -lcrypto
with no luck as well.
/lib/i386-linux-gnu/libcrypto.so.1.0.0
我有点不知所措.如果有人想让我觉得自己像个傻瓜,我会很高兴弄清楚我做错了什么!
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!
推荐答案
事实证明这是愚蠢的.在链接器步骤中,我使用了 gcc -Wall -g -lssl -lcrypto -o program program.o
.我需要将库链接移动到我正在链接的目标文件之后,并将 libssl
放在 libcrypto
之前:
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
这篇关于在 GCC 中链接 libssl 和 libcrypto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!