Keeping file hierarchy across subdirectories in CMake

前端 未结 3 1261
北恋
北恋 2020-12-06 12:57

Till date I still do not really understand what the \'best practice\' is for doing this for a CMake project with many subdirectories.

Say I have a project hierarchy

3条回答
  •  悲哀的现实
    2020-12-06 13:32

    There is a fourth way if you're using newer versions of CMake.

    Take a look at target_sources() command of CMake.

    It seems like you are declaring your target in your CMakeLists.txt

    add_executable(my_target "subd1/CMakeLists.txt" "subd2/CMakeLists.txt")
    add_subdirectory(subd1)
    add_subdirectory(subd2)
    

    Instead of propagating your Source files up to the root you can depend on the target you have defined in the root CMakeLists.txt. That means subd1/CMakeLists.txt may look like:

    target_sources(my_target PRIVATE "subd1/Source.cpp" "subd1/Source2.cpp")
    

    [EDIT]

    As stated in the comments you must give the relative path of the source-files to target_sources(). I use target_sources() because I do not want the explicit source file listing to pollute the targets CMakeLists.txt. Another use case is that target_sources() can be invoked with the PUBLIC or INTERFACE keyword to propagate source files to depending targets. Well I never used target_sources() that way.

    [/EDIT]

    If you're using IDEs like Visual Studio that support folders you make want to also declare a source_group() in the CMakeLists.txt that contains your target. So the root CMakeLists.txt may look like:

    add_executable(my_target "subd1/CMakeLists.txt" "subd2/CMakeLists.txt")
    add_subdirectory(subd1)
    add_subdirectory(subd2)
    ...
    source_group(subd1 REGULAR_EXPRESSION "subd1/*")
    source_group(subd2 REGULAR_EXPRESSION "subd2/*")
    

    I'm using this approach because it leads to much cleaner CMakeLists.txt files, its lesser work and I think the introduction of not needed variables only raises the complexity of your CMakeLists.txt files.

    CMakeLists.txt as target sources

    I currently use the CMakeLists.txt of the sub folders as source files of the target because otherwise CMake will complain that the add_executable command has no source files given.

提交回复
热议问题