链接C文件错误:链接器命令失败,退出代码为1

时间:2015-09-22 02:36:43

标签: c linker

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

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

编辑:我在rsa-test.c中包含了rsa.c和prf.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中的函数,这就是造成这个问题的原因。

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

2 个答案:

答案 0 :(得分:3)

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

您似乎正在使用GMP,因此该标志应为-lgmp

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

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

答案 1 :(得分:1)

链接时你应该包含库或共享库以及带有-L选项的optionla库路径,这样的一个例子

gcc -o binary *.o -L /path/to/libraries -llibrary

其中-llibrary看起来像这样

liblibrary.so or liblibrary.so*