问题描述
使用 OpenSSL 库函数编程时,采用 gcc 命令编译运行.c
程序,可以成功编译,生成.o
文件。但是无法运行,生成可执行文件,产生如下错误。
/usr/bin/ld: /tmp/: 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/: in function `https_read':
test02_ssl_https.c:(.text+0x5dd): undefined reference to `SSL_read'
/usr/bin/ld: /tmp/: in function `https_write':
test02_ssl_https.c:(.text+0x637): undefined reference to `SSL_write'
/usr/bin/ld: /tmp/: in function `https_get_status_code':
test02_ssl_https.c:(.text+0x702): undefined reference to `SSL_read'
/usr/bin/ld: /tmp/: in function `https_read_content':
test02_ssl_https.c:(.text+0x87d): undefined reference to `SSL_read'
/usr/bin/ld: /tmp/: 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/: 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 -o ot -lcrypto -lssl
运行上述命令之后,能够成功产生可执行文件,执行./
命令成功执行。