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

Leg*_*end 3 osx libraries gcc linker 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
Run Code Online (Sandbox Code Playgroud)

库文件是:

/usr/lib/libssl.0.9.7.dylib
/usr/lib/libssl.0.9.8.dylib
/usr/lib/libssl.dylib
Run Code Online (Sandbox Code Playgroud)

我尝试了几个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
Run Code Online (Sandbox Code Playgroud)

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

这样做的正确方法是什么?

Chr*_*man 5

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

find /usr/local/Cellar/ -name "libssl.*"
Run Code Online (Sandbox Code Playgroud)

你应该找到类似 /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
Run Code Online (Sandbox Code Playgroud)