Linking C files Error: linker command failed with exit code 1

Linking C files Error: linker command failed with exit code 1

我有三个文件,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 中的函数,这就是导致此问题的原因。

如果您想测试我的文件,请告诉我,我会 post 上传它们。

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

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

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

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

链接时,您应该包括库或共享库以及带有 -L 选项的选项库路径,例如

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

其中 -llibrary 看起来像这样

liblibrary.so or liblibrary.so*