42

I'm on a mac and I used homebrew to install gmp.

Kyumins-iMac:gcjlib math4tots$ g++ main.cpp -lgmp -lgmpxx
In file included from main.cpp:2:
./gcjlib.hpp:4:10: fatal error: 'gmpxx.h' file not found
#include <gmpxx.h>
         ^
1 error generated.

So then I explicitly told g++ to use /usr/local/include

Kyumins-iMac:gcjlib math4tots$ g++ main.cpp -lgmp -lgmpxx -I/usr/local/include
ld: library not found for -lgmp
clang: error: linker command failed with exit code 1 (use -v to see invocation)

So then I explicitly told g++ to use /usr/local/lib

Kyumins-iMac:gcjlib math4tots$ g++ main.cpp -lgmp -lgmpxx -I/usr/local/include -L/usr/local/lib
Kyumins-iMac:gcjlib math4tots$ ./a.out 
sum is -4444
absolute value is 4444

So the only issue seems to be that g++ fails to acknowledge /usr/local.

But it is tedious to type all this out all the time, especially when I'm just writing small single file programs.

Is there a way for me to get g++ to acknowledge the stuff in /usr/local by default? Is there a standard way homebrew users do this?


I'm on OS X 10.9.3 with Xcode 5.1.1 in case it is relevant.

4
  • What about using a makefile? May 28, 2014 at 8:12
  • 7
    Please stop saying g++, the program you are calling is clang++ (through a confusing alias provided by apple) and your question has nothing to do with gcc. If you do install a true gcc, it is likely to work as you expect. May 28, 2014 at 8:59
  • $ sudo rm -rf Applications/Xcode.app worked for me in 10.12.6. Nov 16, 2017 at 16:01
  • You can use g++ --version to see what you're actually running.
    – Richard
    Jun 28, 2019 at 21:47

7 Answers 7

62

I also use Homebrew and had a similar problem on Mac OSX Maverick 10.9.5 and Xcode 6.0.1, but it was solved by running:

xcode-select --install

Note that it doesn't work without the double hyphens given by the previous answer. This installs the command-line tools that also create /usr/lib/ and /usr/include/. I don't know why Homebrew doesn't automatically check this upon installation, since it does check for Xcode...

If you want to check exactly what folders your compiler is looking through for header files you can write:

cpp -v

4
  • 1
    My hero! I've been breaking my head for the past 16 hours, solved with just one line. Mar 16, 2016 at 10:57
  • Struggling to understand why Apple would stop searching /usr and /usr/local by default, but in any case, this fixes some of my build problems too.
    – Weston C
    Oct 21, 2016 at 19:53
  • True hero there! Jul 28, 2017 at 17:11
  • I guess you meant: This installs the command-line tools that also search /usr/lib/ and /usr/include/ ? Nov 10, 2017 at 13:56
17

A workaround would be to:

export C_INCLUDE_PATH=/usr/local/include
export CPLUS_INCLUDE_PATH=/usr/local/include

At least this tricked the pre-processor to behave here :)

3
  • 1
    This worked for me building nginx, although openssl/ssl.h was in /opt/local/include, so I used that path.
    – Josh Wulf
    Dec 31, 2015 at 7:27
  • It might also be useful to export LIBRARY_PATH.
    – John Perry
    Sep 5, 2019 at 23:17
  • with cmake, include_directories(/usr/local/include) works too
    – dB.
    Mar 5, 2020 at 18:17
11

Try running xcode-select --install

At least on Mavericks, I've found that if I install the Xcode application without installing the command-line tools, then the tools are sort of available, but normal unix-ey builds don't work correctly. One symptom is that /usr/local/include is not on the include search path. The command-line tools seem to resolve this issue.

0
2

I have Yosemite 10.10.5 and running xcode-select --install didn't fix the problem for me. The command returned with xcode-select: error: command line tools are already installed, use "Software Update" to install updates.

When I ran xcode-select -p, it showed /Applications/Xcode.app/Contents/Developer. I ended up deleting Xcode from the Applications directory, which resulted in xcode-select -p returning /Library/Developer/CommandLineTools. This fixed compiler error for me.

3
  • When I tried this, clang could no longer find some important standard library files.
    – John Perry
    Sep 5, 2019 at 22:38
  • 3
    It is unclear how the problem is solved from this answer, if you delete Xcode, all the compilers will be gone. For people who see this, do not try it.
    – 1a1a11a
    Nov 18, 2019 at 5:31
  • If you have commandLineTools, you don't need xcode. Mar 18, 2022 at 19:21
2

That was helpful for me:

Use the latest version. 1.0.2o_1 just a current build.

brew install openssl
ln -s /usr/local/Cellar/openssl/1.0.2o_1/include/openssl /usr/local/include/openssl
ln -s /usr/local/Cellar/openssl/1.0.2o_1/lib /usr/local/lib/openssl
0
1

There are a few questions around this topic with answers that suggest putting a symlink in /usr/local/include. However I'm running macOS Monterey 12.3 (on an M1 MacBook) and that directory doesn't exist.

I had installed the Xcode command line tools by downloading the package from Apple, so xcode-select --install just tells me it's already installed and doesn't create any directories.

I ran cpp -v to see which directories are searched for #include <...>:

 /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
 /Library/Developer/CommandLineTools/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)

I picked /Library/Developer/CommandLineTools/usr/include for the symlink. In that directory, I ran the following command (note the new location for Homebrew installations, under /opt/homebrew - some old answers are also out of date on this point):

sudo ln -s /opt/homebrew/opt/openssl@3/include/openssl .

clang was then able to find the OpenSSL files.

-1

apk add --no-cache build-base it works fine !!!! go build -tags musl -o main main.go

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Mar 22, 2022 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.