简体   繁体   中英

C\C++ build linker error in Xcode

I am trying to use this library that implements Paillier encryption and uses the GMP library in Xcode.

To do that I downloaded both libraries and built them successfully. Then I added "usr/local/include" to "Header Search Paths" and "/usr/local/lib" to "Library Search Paths" in the project settings in Xcode. I also added "-lgmp" and "-lpaillier" to "Other Linker Flags".

I can include "gmp.h" and "paillier.h" without problems, and I can use gmp functionalities too. But I get a build error if I try to use paillier functionalities.

Undefined symbols for architecture x86_64:
  "paillier_keygen(int, paillier_pubkey_t**, paillier_prvkey_t**, void (*)(void*, int))", referenced from:
      DOwner::run() in downer.o
  "paillier_get_rand_devrandom(void*, int)", referenced from:
      DOwner::run() in downer.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I tried to find solutions for this problem through google but nothing I found worked.

UPDATE

I switched the flag "-lpaillier" to "-l:libpaillier.a" and received the following error:

ld: library not found for -l:libpaillier.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is strange because the file "libpaillier.a" is in /usr/local/lib with "libgmp.a" so how could it find the gmp file but not this one. To check if Xcode is searching in a different directory I ran sudo find / -name "libgmp.a" to see if there is any other instance of it that Xcode might be using, but there was only one in /usr/local/lib.

UPDATE

To make sure Xcode links to the library I went to "Build Phases" > "Link Binary With Library" and dragged the file "libpaillier.a" to it. But the error persists.

UPDATE

I copied the paillier.h and paillier.c files that constituted the whole source code into my project folder. Then I linked paillier.c to my target project. The problem persisted. But when I renamed the files into paillier.hpp and paillier.cpp because my project is a cpp project the problem was solved. However I get another error that says it's illegal to have void pointer arithmetic which the library uses. Is there a way to allow for void pointer arithmetic in Xcode?

Judging from the first error, most likely you were building the library for 32 bit (I guess you had some makefile for building the lib) and your application for 64 bit, so the linker finds the library, but does not find an implementation for your intended architecture.

So check your project settings in Xcode for the application and change them to 32 bit. Or see if there are build instructions to build the library in 64 bit.

Finally solved it or rather went around it.

Here are the steps I took:

  1. Instead of importing libpaillier as a library I included the source files in my project.
  2. I renamed the extensions to hpp and cpp because the compiler had a problem otherwise.
  3. I replaced every instance with a void pointer that was used in arithmetic to a char *.
  4. I cast some void pointers to char pointers if required.

While this might not be the most elegant solution, it works and I couldn't find another way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM