On mac, g++ (clang) fails to search /usr/local/include and /usr/local/lib by default

梦想与她 提交于 2020-01-08 16:02:06

问题


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.


回答1:


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




回答2:


Try running xcode-select install

At least on Mavericks I've found 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.




回答3:


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 :)




回答4:


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.




回答5:


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


来源:https://stackoverflow.com/questions/23905661/on-mac-g-clang-fails-to-search-usr-local-include-and-usr-local-lib-by-def

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!