matth
matth

Reputation: 573

How can I force cmake to use C++ header files in /usr/include in Linux?

I have a cmake project that uses a header installed in /usr/include, let's call it freeglut.h. When I use find_package(GLUT) I get ${GLUT_INCLUDE_DIR} pointing to /usr/include. All's well.

Now, I'm adding CUDA, which keeps its own copies of these and other headers in one of the paths included with find_package(CUDA). Normally I would resolve this by placing ${GLUT_INCLUDE_DIR} before ${CUDA_INCLUDE_DIRS} in include_directories(). However, on Unix systems cmake, in UnixPaths.cmake, maintains a list called CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES, which contains /usr/include and apparently keeps cmake from emitting -I<dir> arguments for the compiler for the directory /usr/include, meaning that the compiler searches the CUDA path first, and uses the freeglut.h header found there.

I have tried using list(REMOVE_ITEM ... to remove /usr/include from the CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES list, but that didn't change the compiler commands that cmake made.

I could of course start hacking around with the CUDA installation, delete the headers I don't want, or modify the CUDA_INCLUDE_DIRS variable, but is there a clean way to tell cmake to use the system header files first, if they exist?

Upvotes: 3

Views: 4813

Answers (1)

hank
hank

Reputation: 9853

I suppose the -idirafter flag should help you:

-idirafter dir

Search dir for header files, but do it after all directories specified with -I and the standard system directories have been exhausted. dir is treated as a system include directory.

https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html

You can use it like this to lower CUDA include dirs priority:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -idirafter /usr/include/<CUDA_includes>")

Upvotes: 3

Related Questions