[CMake] How to add project files?

Florian Lindner mailinglists at xgm.de
Thu Jul 13 22:33:12 EDT 2017


Hello,

our project, which is currently using scons is structured like this

ROOT
  - SConstruct
  - CMakeLists.txt
  - src/
    - SConscript
    - CMAkeLists.txt
    - Module1
      - somesourcefiles.cpp
      - tests/
        - testsourcefiles.cpp
      - config/
         - configsourcefile.cpp
     - Module2 (like Module1)
     - ...

Right now the SConstruct calls SConscript

sourcesModule1 = [
    Glob('Module1/*.cpp'),
    Glob('Module2/config/*.cpp')
]

The test files (Boost Test) are compiled to a standalone executable

sourcesTests = [
    Glob('*/tests/*.cpp'),
    File("testing/main.cpp")
]

We currently have 4 targets. A static and dynamic lib (comprised of all modules), a binary (comprised of all modules +
main.cpp) and the test binary (all modules, tests and test main.cpp).

I was able to replicate that structure with CMake and it builds fine:


src/CMakeLists.txt:

file(GLOB sourcesAllNoMain
  "Module1/*.cpp"
  "Module1/config/*.cpp")

file(GLOB sourcesTests
  "*/tests/*.cpp")

set (sourcesAllNoMain ${sourcesAllNoMain} PARENT_SCOPE)
set (sourcesTests ${sourcesTests} PARENT_SCOPE)


ROOT/CMakeLists.txt:

add_subdirectory("src")

add_library(solib ${sourcesAllNoMain})
set_target_properties(solib
  PROPERTIES OUTPUT_NAME libprecice)
target_link_libraries(solib ${PYTHON_LIBRARIES})
target_link_libraries(solib ${MPI_LIBRARIES})
target_link_libraries(solib ${Boost_LIBRARIES})
target_link_libraries(solib ${petsc})


Now, in the docs I everywhere read not to add the files using GLOB. However, I have found no definitive guide how to add
project files in a large project with subdirs like that.

How would you do it in cmakeish fashion?

I know this might be a FAQ, but googled for days and found no suitable answer.

Thanks a lot,
Florian


More information about the CMake mailing list