我在源树中有一个名为"time.h"的文件,与系统"time.h"完全相同。这是无法更改的。我在cmake中遇到了一个问题,当我使用include_library选项时,它会被转换为-I标志,这意味着我的自定义"time.h"会占用系统时间.h的优先级,甚至对于<>包括。这是一个明确的否定。
我尝试使用include_directories(AFTER dir1 dir2),但它仍然生成-I选项,而不是预期的-idrafter。
我不认为这是CMake的问题;我相信gcc总是会在系统之前找到你的"time.h",无论你在#include
中使用引号还是括号,也不管include_directories
中有什么选项。请参阅gcc文档中的-I
和-isystem
条目
CMake的include_directories
的AFTER
选项只与gcc命令中列出的目录的顺序有关,而与gcc的-idirafter
标志无关。
拥有与系统文件名称相同的文件不是一个好计划,但如果你手头拮据,你可以在不需要重命名时间的情况下避免这个问题
CMakeLists.txt: include_directories(${PROJECT_SOURCE_DIR}/src)
header file: #include <time.h> // we want but don't get system one
#include "time.h" // we want and get own custom one
更像
CMakeLists.txt: include_directories(${PROJECT_SOURCE_DIR})
header file: #include <time.h> // we want and get system one
#include "src/time.h" // we want and get own custom one
另一种选择是坚持使用当前的#include
设置(使用尖括号表示系统时间.h,使用引号表示您自己的时间),而在CMakeLists.txt中根本不使用include_directories
。相反,我认为您可以用以下内容替换它:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -iquote ${PROJECT_SOURCE_DIR}/src")
使用-iquote
可能是比-idirafter
更好的选择,因为-idirafter
指定的目录(在这种情况下不正确)被视为系统目录,并且因此抑制了警告等。
如果您确实选择了这个选项,那么可能值得对CMakeLists.txt进行注释,以解释为什么没有include_directories
来避免将来的重构恢复为使用更正常的include_directories
命令。
总而言之,如果可能的话,你最好的选择是重命名你的"time.h"文件。