这个答案试图总结 GCC 和 Clang 的搜索路径行为。
海合会
包含路径:对于以下格式的命令行:
CPLUS_INCLUDE_PATH=EDIR g++ -IIDIR -isystemSDIR
以下目录列表用作搜索路径#include <...>
:
IDIR # '-I' directories.
SDIR # '-isystem' directories.
EDIR # *_INCLUDE_PATH directories.
GCCDIR/include/c++/GCCVER # libstdc++ directory (C++).
GCCDIR/include/c++/GCCVER/GCCARCH # libstdc++ directory (C++).
GCCDIR/include/c++/GCCVER/backward # libstdc++ directory (C++).
GCCDIR/lib/gcc/GCCARCH/GCCVER/include # GCC arch-specific directory.
/usr/local/include/GCCARCH # Local arch-specific include directory.
/usr/local/include # Local include directory.
GCCDIR/include # GCC include directory.
GCCDIR/lib/gcc/GCCARCH/GCCVER/include-fixed # GCC include-fixed directory.
/usr/include/GCCARCH # System include arch-specific directory.
/usr/include # System include directory.
库路径:对于以下格式的命令行:
LIBRARY_PATH=EDIR gcc -BBDIR -LLDIR
以下参数被传递给链接器:
-LLDIR # '-L' directories.
-LBDIR # '-B' directories.
-LEDIR/../libXX # Multilib directories from LIBRARY_PATH.
-LGCCDIR/lib/gcc/GCCARCH/GCCVER # GCC arch-specific library directory.
-LGCCDIR/libXX # GCC multilib library directory.
-L/libXX # System multilib library directory.
-L/usr/libXX # System multilib library directory.
-LEDIR # LIBRARY_PATH directories.
-LGCCDIR/lib # Other GCC libraries.
铛
包含路径:对于以下格式的命令行:
CPLUS_INCLUDE_PATH=EDIR clang++ --gcc-toolchain=GCCDIR -BBDIR -IIDIR -isystemSDIR
以下目录列表用作搜索路径#include <...>
:
IDIR # '-I' directories.
SDIR # '-isystem' directories.
EDIR # *_INCLUDE_PATH directories.
# If -stdlib=libstdc++ is used:
GCCDIR/include/c++/GCCVER # libstdc++ directory from the selected GCC toolchain (C++).
GCCDIR/include/c++/GCCVER/GCCARCH # libstdc++ directory from the selected GCC toolchain (C++).
GCCDIR/include/c++/GCCVER/backward # libstdc++ directory from the selected GCC toolchain (C++).
# If -stdlib=libc++ is used:
CLANGDIR/include/c++/v1 # libc++ directory (C++).
/usr/local/include # Local include directory.
CLANGDIR/lib/clang/CLANGVER/include # Clang include directory.
/include # System include directory.
/usr/include # System include directory.
库路径:对于以下格式的命令行:
LIBRARY_PATH=EDIR clang --gcc-toolchain=GCCDIR -BBDIR -LLDIR
以下参数被传递给链接器:
-LLDIR # '-L' directories.
-LGCCDIR/lib/gcc/GCCARCH/GCCVER # GCC arch-specific library directory.
-LGCCDIR/libXX # GCC multilib library directory.
-L/libXX # System multilib library directory.
-L/usr/libXX # System multilib library directory.
-LGCCDIR/lib # Other GCC libraries.
-LCLANGDIR/lib # Clang libraries.
-L/lib # System library directory.
-L/usr/lib # System library directory.
-LEDIR # LIBRARY_PATH directories.
概括
在 GCC 和 Clang 中,include 的搜索路径几乎相同。如果在这两种情况下都使用 C 前端,则省略 C++ 特定路径。GCC 和 Clang 之间的库搜索路径有很大不同,特别是-B
目录的存在和 GCC 前端的奇怪操作LIBRARY_PATH
。
C 和 C++ 前端的库搜索路径相同。其他库搜索路径由链接器本身引入。以下摘录来自 GNU Binutils 的 vanilla 链接描述文件:
# Multilib library directories.
SEARCH_DIR("BINUTILSDIR/BINUTILSARCH/libXX");
SEARCH_DIR("BINUTILSDIR/libXX");
SEARCH_DIR("/usr/local/libXX");
SEARCH_DIR("/libXX");
SEARCH_DIR("/usr/libXX");
# Traditional library directories.
SEARCH_DIR("BINUTILSDIR/BINUTILSARCH/lib");
SEARCH_DIR("BINUTILSDIR/lib");
SEARCH_DIR("/usr/local/lib");
SEARCH_DIR("/lib");
SEARCH_DIR("/usr/lib");
还必须注意,不在上面列出的目录中搜索库依赖项。这些完全依赖于传递给链接器的选项,否则它们会从默认的系统库路径中解析。因此,同时生成和参数以确保链接正确的库可能很有用。-rpath
-rpath-link
-L
-rpath-link
-B
最后,仅在目录中搜索特殊文件(例如 CRT 对象) 。在 Clang 中,还会在选定的 GCC 工具链中搜索特殊文件。其他因素(规范文件、特定于发行版的配置)可能会改变上述部分或全部内容。