我不认为这是 CMake 的问题;我相信 gcc 总是会在系统之前找到你的“time.h”,无论你在中使用引号还是括号#include
并且无论其中的各种选项如何include_directories
。请参阅条目-I
and -isystem
in the 海湾合作委员会文档
The AFTER
CMake 的选项include_directories
仅与 gcc 命令中列出的目录顺序相关,与 gcc 无关-idirafter
flag.
让您自己的文件与系统文件同名并不是一个好计划,但如果您束手无策,您可以通过更全面地限定您自己的包含路径来避免此问题,而无需重命名 time.h ,因此而不是例如
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
设置(使用系统 time.h 的尖括号和您自己的引号)而不是使用include_directories
全部在 CMakeLists.txt 中。相反,我认为你可以用类似的东西替换它:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -iquote ${PROJECT_SOURCE_DIR}/src")
Using -iquote
可能是一个更好的选择-idirafter
由于指定的目录-idirafter
被(在这种情况下错误地)视为系统目录,因此抑制了警告等。
如果您确实选择此选择,可能值得对 CMakeLists.txt 进行评论以解释为什么没有include_directories
以避免将来的重构恢复到使用更正常的状态include_directories
命令。
总而言之,如果可能的话,最好的选择是重命名“time.h”文件。