架构 arm64 的未定义符号:_BN_new

时间:2021-06-05 18:08:32

标签: c macos gcc

我正在尝试编译这个超级简单的代码:

#include <openssl/bn.h>

int main(void)
{
    BIGNUM *k = BN_new();
}

我使用以下命令:

gcc-11 test.c -I/opt/homebrew/opt/openssl@1.1/include -L/opt/homebrew/opt/openssl@1.1/lib

我明白

gcc-11 test.c -I/opt/homebrew/opt/openssl@1.1/include -L/opt/homebrew/opt/openssl@1.1/lib
Undefined symbols for architecture arm64:
  "_BN_new", referenced from:
      _main in ccRnO859.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status

这里有更多信息:

% file $(which gcc-11)
/opt/homebrew/bin/gcc-11: Mach-O 64-bit executable arm64
% gcc-11 --version                                                       
gcc-11 (Homebrew GCC 11.1.0) 11.1.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% file /opt/homebrew/opt/openssl@1.1/lib/libcrypto.dylib           
/opt/homebrew/opt/openssl@1.1/lib/libcrypto.dylib: Mach-O 64-bit dynamically linked shared library arm64
% nm -gU /opt/homebrew/opt/openssl@1.1/lib/libcrypto.dylib|grep _BN_new  
00000000000311bc T _BN_new

编辑:我对 x86_64 版本的 GCC 和 OpenSSL 进行了完全相同的尝试,结果相同

1 个答案:

答案 0 :(得分:1)

如您所见,此函数在 libcrypto 库中定义,但您实际上并未与该库链接。您需要将 -lcrypto 添加到链接器命令行的末尾。

-L 选项指定要搜索的目录,以查找使用 -l 选项请求的库,但本身不向链接添加任何库。