【问题标题】:compiling with libssl does not work用 libssl 编译不起作用
【发布时间】:2018-05-24 22:10:34
【问题描述】:

我有一个非常简单的代码:

#include <openssl/sha.h>

int main() {
    SHA_CTX sha1;
    SHA_Init(&sha1);
}

我已经安装了libssl-dev 和libcrypto++-dev:

但是我使用以下命令构建失败:

$ gcc -lcrypto -lssl main.c
/tmp/ccfnCAxT.o: In function `main':
main.c:(.text+0x1f): undefined reference to `SHA1_Init'
collect2: error: ld returned 1 exit status
$
$ gcc -lssl main.c
/tmp/ccfnCAxT.o: In function `main':
main.c:(.text+0x1f): undefined reference to `SHA1_Init'
collect2: error: ld returned 1 exit status

平台:Ubuntu 16.04

【问题讨论】:

  • 你需要在最后指定库。试试gcc main.c -lcrypto -lssl

标签: c gcc openssl libssl


【解决方案1】:

-lssl不用,-lcrypto就够了,一定要放在最后:

gcc -o main main.c -lcrypto

(或者你希望你的程序在-o之后被调用)

【讨论】:

  • 谢谢!你知道为什么 -lcrypto 应该放在最后吗?
  • @milaniez 从历史上看,当链接过程中先前或当前的目标代码未引用它们时,链接器倾向于丢弃每个模块的符号,但在引用但未解决的稍后链接的目标代码中解析外部符号尚未解决。换句话说,main.o 需要 SHA1 入口点,但它们还没有解决,所以在即将到来的目标代码中寻找它们。值得庆幸的是,大多数链接器不再追求这种疯狂,而是真正解决了前后问题。
猜你喜欢
  • 2011-10-29
  • 2018-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多