Include search path on Mac OS X Yosemite 10.10.1

本秂侑毒 提交于 2019-12-02 21:06:57
jww

I'd like to change library search path...

At compile time, you augment the library search path with -L. You cannot delete paths; you can only add paths that have a "higher" preference than existing paths.

At runtime, you use DYLD_LIBRARY_PATH, DYLD_FALLBACK_LIBRARY_PATH and friends to change the library search path. See dyld(1) OS X Man Pages.

Usually you use DYLD_LIBRARY_PATH to ensure a particular library is loaded rather than the system one. Its a way to override default behavior. DYLD_FALLBACK_LIBRARY_PATH is used to provide a library that's not a system one. Its less intrusive because you don't displace system paths.

----------

I need /usr/local/include first...

Sounds a bit odd to me, but...

$ export DYLD_LIBRARY_PATH=/usr/local/include
$ clang ...

----------

IF you are cross-compiling from the command line, you should do something like this. The trick is to use --sysroot to automatically bring in the headers and libraries for the platform, rather than hacking them with -I, -L and -l.

# Put cross compile tools on the PATH first
export PATH="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/:$PATH"

# C compiler
export CC=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang

# C++ compiler
export CXX=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++

# SYSROOT brings in platform headers and libraries, No need for -I, -L and -l.
SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk

# Compiler flags
export CFLAGS="-march armv7 -march armv7s --sysroot=$SYSROOT"

# Compiler flags
export CXXFLAGS="-march armv7 -march armv7s --sysroot=$SYSROOT"

# Using defualt C++ runtime
$CXX $CXXFLAGS foo.c -o foo.exe

You can also specify GNU's runtime with -stdlib=libstdc++, and LLVM's runtime with -stdlib=libc++.

----------

Based on your updates:

<ld command and output omitted>
where does this path /usr/include/c++/4.2.1 come from??

/usr/include/c++/4.2.1 is not in your Library Search Path. Its just not there.

That's a header include path, and you specify it with -I.

If you want to see your header include paths for libstdc++ and libc++, do this:

# GNU C++ runtime
$ echo | /usr/local/bin/clang++ -Wp,-v -stdlib=ibstdc++ -x c++ - -fsyntax-only

And:

# LLVM C++ runtime
$ echo | /usr/local/bin/clang++ -Wp,-v -stdlib=libc++ -x c++ - -fsyntax-only

Here's what I get on OS X 10.8.5:

libstdc++ (GNU):

 /usr/include/c++/4.2.1
 /usr/include/c++/4.2.1/backward
 /usr/local/include
 /usr/local/bin/../lib/clang/3.5.0/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory

libc++ (LLVM):

 /usr/local/include
 /usr/local/bin/../lib/clang/3.5.0/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)

So /usr/include/c++/4.2.1 is a built-in compiler path when using GNU's libstdc++ (but not LLVM's libc++). I'll go even further and tell you its hard coded in LLVM/Clang sources (I build LLVM/Clang from source often):

$ cd clang-sources/llvm
$ grep -R "/usr/include/c++/4.2.1" *
tools/clang/lib/Frontend/InitHeaderSearch.cpp:      AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
tools/clang/lib/Frontend/InitHeaderSearch.cpp:      AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
tools/clang/lib/Frontend/InitHeaderSearch.cpp:      AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
tools/clang/lib/Frontend/InitHeaderSearch.cpp:      AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
tools/clang/lib/Frontend/InitHeaderSearch.cpp:      AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",

You can add additional include paths when building LLVM/Clang with --with-c-include-dirs.

----------

Based on your comment: "... If I delete ~/anaconda/include/hdf5.h...". If you need a new path for the HDF5 package, then use something like:

export CPPFLAGS="-I/path/to/preferred/HDF5/hdf5.h"
export CFLAGS="-I/path/to/preferred/HDF5/hdf5.h"
export CXXFLAGS="-I/path/to/preferred/HDF5/hdf5.h"
export LDFLAGS="-L/path/to/preferred/HDF5/lib"
./configure ...

You need it in CPPFLAGS too because the autotools might run some tests with it.

Then, at runtime:

export DYLD_LIBRARY_PATH=/path/to/preferred/HDF5/lib
./run_the_executable

And you can side step DYLD_LIBRARY_PATH IF you perform static linking against HDF5. But be aware the OS X linker always uses a shared object or dynalib if its available. Effectively, it ignores your request for static linking. You will have to mv shared object or dynalib off-path while leaving an archive on-path.

----------

To muddy the waters a little more... If you are saying "library" when you really want to say "framework", then see this Stack Overflow question about using the -framework option: Clang(LLVM) compile with frameworks.

You can go into your project settings and make changes to edit your library search paths very easily.

See how I opened up the settings in this test project screenshot? There's no paths in my own project there right now; but in your project, there will certainly be duplicate paths (or multiple paths that resolve to the same place).

Eman Jayme
  • Open Terminal
  • Run gcc -V
  • Read the information and copy the include path. Mine is /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!