如何使用 gcc 在 macOS 中链接 OpenSSL 库?

如何使用 gcc 在 macOS 中链接 OpenSSL 库?

我通过 Homebrew 包管理器安装了 OpenSSL。我已经找到了我需要的库和头文件。

标题是:

/usr/local/Cellar/openssl/1.0.2h_1/include/openssl
/usr/local/Cellar/openssl/1.0.2j/include/openssl
/usr/local/Cellar/openssl/1.0.2k/include/openssl

库文件是:

/usr/lib/libssl.0.9.7.dylib
/usr/lib/libssl.0.9.8.dylib
/usr/lib/libssl.dylib

我尝试了几个gcc命令来尝试链接 OpenSSL 库,包括:

gcc md5.c -I/usr/local/Cellar/openssl/1.0.2k/include -lssl
gcc md5.c -I/usr/local/Cellar/openssl/1.0.2k/include -llibssl
gcc md5.c -I/usr/local/Cellar/openssl/1.0.2k/include -llibssl.dylib
gcc md5.c -I/usr/local/Cellar/openssl/1.0.2k/include -llibssl.0.9.8
gcc md5.c -I/usr/local/Cellar/openssl/1.0.2k/include -llibssl.0.9.8.dylib

所有这些都会产生“找不到文件”错误或链接器错误。

执行此操作的正确方法是什么?

答案1

看起来您正在尝试链接与操作系统一起安装的 openssl 库,而不是自制库。尝试查找 homebrew 安装 1.0.2k 库的位置。

find /usr/local/Cellar/ -name "libssl.*"

您应该找到类似 /usr/local/Cellar/_path_of some_sort/libssl.a 的内容。尝试链接到该库而不是 /usr/lib 中的库。 /usr/lib 库很旧,与您正在使用的头文件不兼容。

gcc md5.c -I/usr/local/Cellar/openssl/1.0.2k/include -L/usr/local/Cellar/path_of_some_sort/ -lssl -lcrypto -o md5

相关内容