cmake include_directories order AFTER/BEFORE

前端 未结 1 2097
闹比i
闹比i 2021-02-11 04:24

I have a file in source tree which name is \"time.h\", exactly as system \"time.h\". This cannot be changed. I have encountered a problem with cmake that when I use include_libr

1条回答
  •  情歌与酒
    2021-02-11 05:15

    I don't think this is a problem with CMake; I believe gcc will always find your "time.h" before the system one, regardless of whether you use quotes or brackets in the #include and regardless of the various options in include_directories. See the entries for -I and -isystem in the gcc documentation

    The AFTER option of CMake's include_directories relates only to the order of the directories as listed in the gcc command, it doesn't relate to gcc's -idirafter flag.

    It's not a great plan to have your own files with identical names to system files, but if your hands are tied, you could avoid this issue without renaming time.h by qualifying the path for your own includes more fully, so rather than e.g.

    CMakeLists.txt:  include_directories(${PROJECT_SOURCE_DIR}/src)
    
    header file:     #include   // we want but don't get system one
                     #include "time.h"  // we want and get own custom one
    

    something more like

    CMakeLists.txt:  include_directories(${PROJECT_SOURCE_DIR})
    
    header file:     #include       // we want and get system one
                     #include "src/time.h"  // we want and get own custom one
    


    An alternative option would be to stick with your current #include setup (using angle brackets for the system time.h and quotes for your own) and not use include_directories at all in the CMakeLists.txt. Instead I think you could replace it with something like:

    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -iquote ${PROJECT_SOURCE_DIR}/src")
    

    Using -iquote is probably a better option than -idirafter since the directory specified by -idirafter is (in this case incorrectly) treated as a system directory, and as such has warnings suppressed, etc.

    If you do go for this choice, it's probably worth commenting the CMakeLists.txt to explain why there is no include_directories to avoid future refactoring reverting back to use the more normal include_directories command.

    All in all, your best option if at all possible would be to rename your "time.h" file though.

    0 讨论(0)
提交回复
热议问题