使用 OpenSSL 库,gcc 出现报错 undefined reference to 的解决方法

问题描述

使用 OpenSSL 库函数编程时,采用 gcc 命令编译运行.c程序,可以成功编译,生成.o文件。但是无法运行,生成可执行文件,产生如下错误。

/usr/bin/ld: /tmp/ccSAMjsz.o: in function `https_init':
test02_ssl_https.c:(.text+0x4bf): undefined reference to `TLS_method'
/usr/bin/ld: test02_ssl_https.c:(.text+0x4c7): undefined reference to `SSL_CTX_new'
/usr/bin/ld: test02_ssl_https.c:(.text+0x4fd): undefined reference to `SSL_new'
/usr/bin/ld: test02_ssl_https.c:(.text+0x538): undefined reference to `SSL_set_fd'
/usr/bin/ld: test02_ssl_https.c:(.text+0x558): undefined reference to `SSL_connect'
/usr/bin/ld: /tmp/ccSAMjsz.o: in function `https_read':
test02_ssl_https.c:(.text+0x5dd): undefined reference to `SSL_read'
/usr/bin/ld: /tmp/ccSAMjsz.o: in function `https_write':
test02_ssl_https.c:(.text+0x637): undefined reference to `SSL_write'
/usr/bin/ld: /tmp/ccSAMjsz.o: in function `https_get_status_code':
test02_ssl_https.c:(.text+0x702): undefined reference to `SSL_read'
/usr/bin/ld: /tmp/ccSAMjsz.o: in function `https_read_content':
test02_ssl_https.c:(.text+0x87d): undefined reference to `SSL_read'
/usr/bin/ld: /tmp/ccSAMjsz.o: in function `https_uninit':
test02_ssl_https.c:(.text+0x938): undefined reference to `SSL_shutdown'
/usr/bin/ld: test02_ssl_https.c:(.text+0x961): undefined reference to `SSL_CTX_free'
/usr/bin/ld: /tmp/ccSAMjsz.o: in function `main':
test02_ssl_https.c:(.text+0x9ef): undefined reference to `OPENSSL_init_ssl'
collect2: error: ld returned 1 exit status

环境介绍

系统环境:Ubuntu 20.04
编译器:gcc 9.3.0
OpenSSL 版本:OpenSSL 1.1.1

问题解决

尝试了很多方法都没有解决,最终发现需要在编译的命令后面加上所使用的库lib的名称。

gcc -I/home/share/pkp/include -L/home/share/pkp/lib -Wall -O3 -Wpedantic -Wno-unused-result -Wno-pointer-sign ot.c -o ot -lcrypto -lssl

运行上述命令之后,能够成功产生可执行文件,执行./命令成功执行。

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值