Add Source in a subdirectory to a cmake project

后端 未结 3 1195
南方客
南方客 2020-12-02 14:30

I have project which has not been divided into libraries, but the source is organized in a directory tree. I do not know how to tell cmake to go down a directory, then add

相关标签:
3条回答
  • 2020-12-02 14:59

    Can't you just set all your sources in project/source/CMakelists.txt then?

    Anyway, what you need is PARENT_SCOPE or CACHE option on set command.

    0 讨论(0)
  • 2020-12-02 15:01

    Since CMake 3.1 there is a new way to add source from subdirectories: target_sources

    Say you have root_dir and root_dir/sub_dir and source files in both. With target_sources you can do this:

    In root_dir/CMakeLists.txt define the target

    add_library(some_target main.cpp)
    add_subdirectory(sub_dir)
    

    In root_dir/sub_dir/CMakeLists.txt add sources:

    target_sources(some_target PRIVATE more_cool_stuff.cpp)
    

    some_target will now contain both source files.

    It is also possible to use other commands in root_dir/sub_dir/CMakeLists.txt using some_target, for example target_compile_definitions which is quite convenient.

    I learned about target_sources here, check it out if you want more explanation and examples

    0 讨论(0)
  • 2020-12-02 15:02

    Like the second part of arrowdodger's answer says: in project/source/folder1/CMakeLists.txt:

    set(SOURCE
       ${SOURCE}
       ${CMAKE_CURRENT_SOURCE_DIR}/file1.cpp
       ${CMAKE_CURRENT_SOURCE_DIR}/file2.cpp
       PARENT_SCOPE
    )
    set(HEADERS
       ${HEADERS}
       ${CMAKE_CURRENT_SOURCE_DIR}/file1.hpp
       ${CMAKE_CURRENT_SOURCE_DIR}/file2.hpp
       PARENT_SCOPE
    )
    
    0 讨论(0)
提交回复
热议问题