本文介绍了在CMake中排除系统包含路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 有没有办法告诉CMake排除某些系统包含路径?Is there a way to tell CMake to exclude some system include paths?例如,我定义了string.h两次,一次在/ usr / include / c ++ / v1(libc ++)中,一次在/ usr / include /中,当我编译时我遇到以下错误:For example I have string.h defined twice, once in /usr/include/c++/v1 (libc++) and once in /usr/include/ and when I compile I have the following error:error: functions that differ only in their return type cannot be overloaded因为 char * strchr(const char * __ s,int __c)函数是我只想使用libc ++中的文件。I'd like to only use the one from libc++.我尝试了 set(CMAKE_IGNORE_PATH / usr / include),但是当我用冗长的模式用clang编译时,仍然在搜索路径中看到 / usr / include。I tried set(CMAKE_IGNORE_PATH "/usr/include") but when I compile with clang in verbose mode I still see "/usr/include" used in search paths.编辑:使用以下CMakeLists.txtWith the following CMakeLists.txtproject(my_lib)set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v")set(SOURCES my_lib.cpp)add_library(my_lib STATIC ${SOURCES})set(CMAKE_DEBUG_TARGET_PROPERTIES INCLUDE_DIRECTORIES)我有以下输出:[ 50%] Building CXX object CMakeFiles/my_lib.dir/my_lib.cpp.oclang version 4.0.1-6 (tags/RELEASE_401/final)Target: x86_64-pc-linux-gnuThread model: posixInstalledDir: /usr/binFound candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.2.0Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0Candidate multilib: .;@m64Selected multilib: .;@m64 "/usr/lib/llvm-4.0/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name my_lib.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -v -dwarf-column-info -debugger-tuning=gdb -coverage-notes-file /media/sf_wncd-app/wasm/cmake/build_linux/CMakeFiles/my_lib.dir/my_lib.cpp.gcno -resource-dir /usr/lib/llvm-4.0/bin/../lib/clang/4.0.1 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/x86_64-linux-gnu/c++/7.2.0 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/x86_64-linux-gnu/c++/7.2.0 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/backward -internal-isystem /usr/include/clang/4.0.1/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-4.0/bin/../lib/clang/4.0.1/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /media/sf_wncd-app/wasm/cmake/build_linux -ferror-limit 19 -fmessage-length 228 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o CMakeFiles/my_lib.dir/my_lib.cpp.o -x c++ /media/sf_wncd-app/wasm/cmake/my_lib.cppclang -cc1 version 4.0.1 based upon LLVM 4.0.1 default target x86_64-pc-linux-gnuignoring nonexistent directory "/include"ignoring duplicate directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/x86_64-linux-gnu/c++/7.2.0"ignoring duplicate directory "/usr/include/clang/4.0.1/include"#include "..." search starts here:#include <...> search starts here: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0 /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/x86_64-linux-gnu/c++/7.2.0 /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/backward /usr/include/clang/4.0.1/include /usr/local/include /usr/include/x86_64-linux-gnu /usr/includeEnd of search list.[100%] Linking CXX static library libmy_lib.a[100%] Built target my_lib当我使用时:project(my_lib)set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v")set(SOURCES my_lib.cpp)set(CMAKE_IGNORE_PATH "/usr/include")add_library(my_lib STATIC ${SOURCES})set(CMAKE_DEBUG_TARGET_PROPERTIES INCLUDE_DIRECTORIES)我得到的输出完全相同。 / usr / include 仍然存在。我猜CMAKE_IGNORE_PATH不适用于系统路径?I got the exact same output. /usr/include is still there. I guess CMAKE_IGNORE_PATH doesn't work for system path?推荐答案如果我使用 clang使用了错误的系统包含目录,那么CMake的答案将是使用 CMAKE_< LANG> _COMPILER_EXTERNAL_TOOLCHAIN 来指定一个自定义GCC工具链。If I take clang is using the wrong system include directory, then CMake's answer would be to use CMAKE_<LANG>_COMPILER_EXTERNAL_TOOLCHAIN to specify a custom GCC toolchain. 参考 Clang开发人员-致力于便捷的交叉编译CMakeClang Developers - Towards convenient cross compiling with CMake 这篇关于在CMake中排除系统包含路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!