Including directories in Clion

前端 未结 2 1463
迷失自我
迷失自我 2020-12-17 02:05

Whenever I wanted to include a directory that was located outside of my project with Clion I would use the -I somedir flag. This time however, what I want to do

相关标签:
2条回答
  • 2020-12-17 02:21

    Command INCLUDE_DIRECTORIES doesn't add any source file for compile!

    Instead, this command defines directories for search header files.

    You need to list all source files in add_executable() call in any case:

    include_directories(src)
    set(SOURCE_FILES
        src/Dijkstra/Dijkstra.cpp
        src/Graph/Graph.cpp)
    add_executable(someprojectname ${SOURCE_FILES})
    
    0 讨论(0)
  • 2020-12-17 02:26

    UPDATE: @Tsyvarev's answer is correct. I've edited this answer to remove the incorrect part and keep the comments relating to target_include_directories(), but it should be viewed as additional to Tsyvarev's answer.

    INCLUDE_DIRECTORIES(src) will make the src directory get added as a search path to all targets defined from that point on. It does not add sources to any targets. The search path will be relative to the current source directory and CMake will adjust it as appropriate when descending into subdirectories via add_subdirectory(). While this is fine if that's what you want, as the project gets bigger and more complicated, you may find you would prefer to apply the include path settings to just some targets. For that, use target_include_directories() instead:

    target_include_directories(someprojectname "${CMAKE_CURRENT_SOURCE_DIR}/src")
    

    This will have the same effect, but it restricts the use of the added include path to just the someprojectname target. If you later define some other target which doesn't need the include path, it won't be added. This can help prevent situations like unexpected files being picked up if you have deep directory hierarchies and you re-use directory names in different places, for example).

    The target_include_directories() command has additional benefits when applied to library targets because CMake has the ability to carry the include path through to anything you link against that library too. Doesn't sound like much, but for large projects which define and link many libraries, it can be a huge help. There are other target-specific commands which have similar benefits too. This article may give you a bit of a feel for what is possible (disclaimer: I wrote the article). It is more focused on target_sources(), but the discussion around carrying dependencies through to other targets may be useful.

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