I am trying to use GMP and MPFR in an XCode project. I installed Homebrew, and then installed the libraries with brew install gmp
and brew install mpfr
, which seemed to be successful. However, when I #include <gmp.h>
, it says that the file is not found. I saw a similar question here, which suggested that I need to tell the compiler where to find it, but I don't know how to do that and there weren't many details.
Edit: I am on an M1 Mac.
When using homebrew, the default behavior for intel macs is to install homebrew into /usr/local
. This means that headers in that environment are in /usr/local/include
.
The default set of search paths for the compiler include this directory, so if you install homebrew software on intel macs, it will automatically find the appropriate headers in that location. It will also link against software in /usr/local/lib
, so it's really convenient - homebrew software tends to just build and link in this environment.
On silicon macs (m1/m2), the default behavior changes. When you install native packages, they will get installed into /opt/homebrew
.
The header directory will be /opt/homebrew/include
and the library directory will be /opt/homebrew/lib
. Unfortunately neither folder will be part of the default build path, which will cause compilation and linking to fail.
In order to add the header folder to the build settings, you need to go to the build settings
for the target.
In the header search paths
add the header folder /opt/homebrew/include
.
In the library search paths
, add the library folder /opt/homebrew/lib
If you're using Makefiles, the corresponding items would be: -I/opt/homebrew/include
and -L/opt/homebrew/lib
If you're using cmake, the corresponding variables that would need to be set would be: include_directories(/opt/homebrew/include)
and link_directories(/opt/homebrew/lib)
.
finally…
If you're using rosetta2 versions of homebrew (these would be intel binaries running on silicon macs), then these versions will get installed into /usr/local
, and will build; as long as you're building binaries for intel. This is a less than trivial situation. There are a lot of things that end up being trickier in this environment - well beyond this answer.