如何强制cmake在/usr/include中使用c++头文件?

时间:2021-11-16 02:41:23

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.

我有一个cmake项目,它使用安装在/usr/include中的header,我们称它为freeglut.h。当我使用find_package(供过于求)时,我得到${GLUT_INCLUDE_DIR}指向/usr/ include_dir)。终成眷属。

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.

现在,我添加CUDA,它在find_package(CUDA)包含的路径中保留自己的这些和其他头部的副本。通常,我将${GLUT_INCLUDE_DIR}放在${CUDA_INCLUDE_DIRS}中的include_directory()中来解决这个问题。但是,在Unix系统cmake中,在UnixPaths中。cmake维护一个名为cmake_cxx_implicit_include_directory的列表,该列表包含/usr/include,并且很明显地防止cmake为目录/usr/include的编译器发出-I

参数,这意味着编译器首先搜索CUDA路径,并使用freeglut。h头发现。

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.

我试过使用list(REMOVE_ITEM…)从cmake_cxx_implicit_include_directory列表中删除/usr/include,但这并没有改变cmake所生成的编译器命令。

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?

我当然可以开始使用CUDA安装,删除我不想要的头文件,或者修改CUDA_INCLUDE_DIRS变量,但是是否有一种干净的方法可以让cmake首先使用系统头文件,如果它们存在的话?

1 个解决方案

#1


3  

I suppose the -idirafter flag should help you:

我想-idirafter旗帜应该对您有所帮助:

-idirafter dir

-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.

为头文件搜索dir,但在使用-I和标准系统目录指定的所有目录都已耗尽之后再执行。dir被视为系统包含目录。

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

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

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

你可以用它来降低CUDA包含dirs优先级:

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

#1


3  

I suppose the -idirafter flag should help you:

我想-idirafter旗帜应该对您有所帮助:

-idirafter dir

-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.

为头文件搜索dir,但在使用-I和标准系统目录指定的所有目录都已耗尽之后再执行。dir被视为系统包含目录。

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

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

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

你可以用它来降低CUDA包含dirs优先级:

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