I'm trying to set up my project to compile correctly using CMake.
My directory looks like this:
root
|- bin
| |- // Where I want to build CMake from - using 'cmake ..'
|- build
| |-
|- include
| |- database
| | |- database.h
|- src
|- database
| |- database.cpp
|- main
|- main.cpp
My sub-directories will definitely grow as my project grows larger and figured CMake would probably a good idea to learn. At the moment I can only get CMake to work with no sub-directories inside my src/. However, I do expect this project to grow into many sub-directories.
Would I need multiple CMakeLists.txt inside each directory with .cpp files? Can anyone point me in the right direction?
Your project does not seem to need multiple CMakeLists.txt. The purpose of having multiple CMakeLists.txt multiple times is multi-fold, but it doesn't seem to fit in your project. For example, one benefit would be hierarchical organization, so that you can separate different units to be built separately, and linked eventually.
Example
Consider the situation if you have another directory in your root called tests
. That directory contains unit tests that you want the developer to have the choice whether to compile it or not. In that case, you put one CMakeLists.txt in tests
, and you enable this in your primary CMakeLists.txt with a add_subdirectory(tests)
as so:
if(testing_enabled)
add_subdirectory(tests)
endif()
In tests
, there would be another CMakeLists.txt. This way, you separate concerns. The development of your unit tests is independent of the development of your project, and also the build process is separate.
Another example
Consider the situation if you have two libraries where the user can choose from. In this case, you have in your root
two directories, lib1
and lib2
. Now you can do something like this in your primary CMakeLists.txt:
if(IWantLib1)
add_subdirectory(lib1)
else()
add_subdirectory(lib2)
endif()
And both lib1
and lib2
directories contain CMakeLists.txt.