c - 链接C文件错误: linker command failed with exit code 1

标签 c linker

我有三个文件:rsa.c、prf.c 和 rsa-test.c。

我使用gcc -c rsa.cgcc -c prf.cgcc -c rsa-test.c来获取.o 文件。

编辑:我将 rsa.c 和 prf.c 包含在 rsa-test.c 中。

然后我使用 gcc -o test rsa.o prf.o rsa-test.o 编译 .o 文件。

编译时出现以下错误。 o 三个文件的文件。

Undefined symbols for architecture x86_64:
  "_EVP_sha512", referenced from:
      randBytes(unsigned char*, unsigned long) in prf.o
  "_HMAC", referenced from:
      randBytes(unsigned char*, unsigned long) in prf.o
  "_SHA512", referenced from:
      setSeed(unsigned char*, unsigned long) in prf.o
  "___gmpz_add_ui", referenced from:
      randBytes(unsigned char*, unsigned long) in prf.o
  "___gmpz_clear", referenced from:
      rsa_shredKey(_RSA_KEY*) in rsa.o
  "___gmpz_export", referenced from:
      zToFile(__sFILE*, __mpz_struct*) in rsa.o
  "___gmpz_import", referenced from:
      zFromFile(__sFILE*, __mpz_struct*) in rsa.o
  "___gmpz_init", referenced from:
      rsa_initKey(_RSA_KEY*) in rsa.o
      setSeed(unsigned char*, unsigned long) in prf.o
  "___gmpz_limbs_read", referenced from:
      randBytes(unsigned char*, unsigned long) in prf.o
  "___gmpz_limbs_write", referenced from:
      rsa_shredKey(_RSA_KEY*) in rsa.o
  "___gmpz_set_ui", referenced from:
      rsa_initKey(_RSA_KEY*) in rsa.o
      setSeed(unsigned char*, unsigned long) in prf.o
  "___gmpz_size", referenced from:
      zToFile(__sFILE*, __mpz_struct*) in rsa.o
      rsa_numBytesN(_RSA_KEY*) in rsa.o
      rsa_shredKey(_RSA_KEY*) in rsa.o
      randBytes(unsigned char*, unsigned long) in prf.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我不确定是什么导致了这个错误。链接器似乎是问题所在,因为我在 rsa-test.c 中使用 rsa.c 和 prf.c 中的函数,这就是导致此问题的原因。

如果您想测试我的文件,请告诉我,我会将其发布。

最佳答案

您需要通过向 gcc -o 命令添加链接标志来链接到您正在使用的库。

看起来您正在使用 GMP,因此该标志应该是 -lgmp

如果涉及的其他库是 OpenSSL,请尝试 -lcrypto 或参见 here如果您从源代码构建它。

gcc -o test_linked.exe rsa.o prf.o rsa-test.o -lgmp -lcrypto

关于c - 链接C文件错误: linker command failed with exit code 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32707511/

相关文章:

c - 从C中的txt文件读取函数?

c - 如何使用 while 循环遍历二叉树?

c++ - lib 中已经定义了标准库,导致链接器错误

c - 在 x86_64 linux 中重定位超过 2GB 的程序时出现链接器错误?

c++ - fwrite 和 write 之间的主要区别是什么?

c - 如何在c中处理tcpdump输出

有效地连接左侧的 N 个字符数组,C

c - 尽管选项正确,静态链接仍不适用于 C++/CLI(未解析的外部符号)

c++ - fedora : undefined reference 上的 gcc 链接器错误

无法使用 ALSA 编译