How to link libraries in Xcode 4?

I'm starting out with Apple Xcode, but I have been unsuccessfully following the Xcode documentation and advice on a few related issues.

I installed GMP in / usr / local / bin, wrote a short program using the library, and compiled it with gcc main.c -lgmp . It compiled without warning or error, and the executable worked flawlessly.

I started a new Xcode project (Command Line Tool, Type: C), copied the code to the newly created main.c and opened the project build settings. From there, I set Linking> Other Linker Flags to -lgmp and the search path> library search path to /usr/local/bin . However, the assembly failed with the preprocessor error "Gmp.h: There is no such file or directory."

I tried almost every headline you could imagine:

#include "gmp.h"

#include <gmp.h>

#include "gmp"

#include "libgmp.a" .,.

This has been my main obstacle in the last three months, which prevented me from finding out C. Any help that leads me to a possible solution would be greatly appreciated.

+4
source share
2 answers

In your Xcode project, you need to configure a few things. For example, I have gmp installed in /opt/gmp/5.0.2 , and I will use this as an example. The actual library is installed in /opt/gmp/5.0.2/lib and the header files in /opt/gmp/5.0.2/include . When you install the library installation, the -PREFIX flag in /opt/gmp/5.0.2 will handle this automatically. If you do not set this flag, the prefix is ​​usually set to /usr/local by default.

  • Other linker flags look correct, this should be the name of the library.
  • Set the header search path to the include directory, in my case /opt/gmp/5.0.2/include .
  • Set the library search path in the lib directory, in my case /opt/gmp/5.0.2/lib .

Since the header search path is set, you should now include the header file as follows:

 #include <gmp.h> 

Of course, replace /opt/gmp/5.0.2 with the PREFIX path that you used when installing gmp.

Finally, you usually do not install libraries on /usr/local/bin , you must install on /usr/local and allow any binaries to be installed in bin, while libraries like these will be installed in lib. Of course, any route scheme will work, I usually recommend /opt/<project-name>/<version-number> , because it allows me to better track what I installed and have several versions of the same libraries and tools without dealing with conflicts.

+8
source

I upgraded my system from a snow leopard to a mountain lion and had to install gmp .

First of all, I installed Xcode CommandLineTools.

Secondly, Homebrew is installed. Then with this I took the steps in this section: https://apple.stackexchange.com/questions/38222/how-do-i-install-gcc-via-homebrew

In my last step, I made changes to the xcode project, as colleague Markus Karlsson said. It finally works! Thank you so much:)

+2
source

All Articles