We have a project P (C/C++ on Linux) consisting of libraries lib1, lib2, lib3.
lib1is standalone linked to another system-wide libslib2links tolib1lib3links to bothlib1andlib2
We have a directory P and extra directories for each of our libs (so, P/lib1/, P/lib2/…). Every library has also its own tests.
Questions:
- Please, how to organize
CMakeLists.txtfor this scenario? - Should we create only one master
builddirectory or one for each lib? - Can we have an option in
CMakeLists.txtforSTATIC vs. SHAREDlinking?
In this case, I would recommend using a single build/ directory. CMake will likely generate separate lib1, lib2 and lib3 directories in there.
Switching between STATIC vs. SHARED can be done by using the BUILD_SHARED_LIBS flag (check the add_library documentation)
With respect to the CMakeLists.txt organization, the choice is yours:
You can build a single CMakeLists.txt which has multiple add_library entries.
This has the benefit that you will get a single CMakeLists.txt, which some people may prefer when the projects are simple.
You could split up your project into multiple CMakeLists.txt distributed over your lib1, lib2 and lib3 directories and use a root cmakelists.txt with add_subdirectory. The benefit of this setup is that it will be easier to generate the build-files with one call (in your build/ directory), but you could then easily step into e.g. lib3/ and call make/msbuild there. CMake will ensure that the dependencies are built correctly
Example 1:
Example 2:
In each subdirectory you then write your CMakeLists.txt. E.g. in case of lib3: