6

Is there a way to tell CMake to exclude some system include paths?

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

because char *strchr (const char *__s, int __c) function is found in each file.

I'd like to only use the one from libc++.

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.

Edit:

With the following CMakeLists.txt

project(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)

I have this output:

[ 50%] Building CXX object CMakeFiles/my_lib.dir/my_lib.cpp.o
clang version 4.0.1-6 (tags/RELEASE_401/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.2.0
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0
Candidate multilib: .;@m64
Selected 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.cpp
clang -cc1 version 4.0.1 based upon LLVM 4.0.1 default target x86_64-pc-linux-gnu
ignoring 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/include
End of search list.
[100%] Linking CXX static library libmy_lib.a
[100%] Built target my_lib

And when I use:

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)

I got the exact same output. /usr/include is still there. I guess CMAKE_IGNORE_PATH doesn't work for system path?

starball
  • 20,030
  • 7
  • 43
  • 238
Thomas
  • 71
  • 2
  • 8
  • Can you please give a [mcve]? Something like this is difficult to reproduce. You can just add `set(CMAKE_DEBUG_TARGET_PROPERTIES INCLUDE_DIRECTORIES)` to check e.g. if it is coming from some definition in your code or a linked package. – Florian Dec 12 '17 at 19:23
  • By the way, my initial issue with duplicate functions comes from the mix of `-stdlib=libc++` and `-fms-compatibility` options when compiling with clang. Without these options, it compiles fine. – Thomas Dec 13 '17 at 13:53
  • I can use `-nostdinc` in clang to not search standard system directories or compiler builtin directories for include files. But this is compiler-dependent. – Thomas Dec 13 '17 at 14:46

1 Answers1

0

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.

References

Florian
  • 39,996
  • 9
  • 133
  • 149