c - macOS 上的 Libsodium,x86_64 的 undefined symbol

标签 c gcc libsodium missing-symbols

我使用的是运行 Catalina 10.15.1 的 Intel Mac

我正在尝试使用 libsodium 使用 gcc Apple clang 版本 11.0.0 (clang-1100.0.33.12)

我都尝试通过 home-brew 安装 libsodium 并手动编译(成功),但是,当尝试使用 libsodium 时> 我收到此错误:

Undefined symbols for architecture x86_64:
  "_crypto_generichash", referenced from:
      _main in sodium-ae2fd0.o
  "_sodium_init", referenced from:
      _main in sodium-ae2fd0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是基本代码,使用libsodium: stable 1.0.18 (bottled), HEAD

  1 #include <sodium.h>
  2
  3 int main(void)
  4 {
  5
  6     sodium_init();
  7
  8     #define MESSAGE ((const unsigned char *) "Arbitrary data to hash")
  9     #define MESSAGE_LEN 22
 10
 11     unsigned char hash[crypto_generichash_BYTES];
 12
 13     crypto_generichash(hash, sizeof hash,
 14                        MESSAGE, MESSAGE_LEN,
 15                        NULL, 0);
 16
 17     return 0;
 18 }

有什么想法吗?

最佳答案

问题可能不在于 libsodium 安装,而在于示例应用程序的编译方式。

为了在编译C程序时链接库(除了隐式链接的C库之外),需要添加-l<library name>标记到编译命令行:

cc -Wall -W -o example example.c -lsodium

关于c - macOS 上的 Libsodium,x86_64 的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59102480/

相关文章:

c - 这个 C 代码是如何工作的。我得到 56 作为 '\08' 的输出

javascript - C 语言与较新语言中的变量名长度

c - 如何将文本文件的内容保存到二维数组中?

c - C中的空语句执行时间

java - 在 java 项目中使用 Libsodium/Kalium

c - 如何从 C 中的 "raw"内存中读取/写入类型值?

c++ - 将 SOIL.lib 与 GCC 一起使用 - 添加符号时出错 : File format not recognised

c++ - GCC 在 lambda 中捕获 `this`

encryption - libsodium xchacha20poly1305 header 敏感吗?

javascript - Libsodium JS KDF 函数产生不同的输出