Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple library CMakeLists.txt

Tags:

We have a project P (C/C++ on Linux) consisting of libraries lib1, lib2, lib3.

  • lib1 is standalone linked to another system-wide libs
  • lib2 links to lib1
  • lib3 links to both lib1 and lib2

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.txt for this scenario?
  • Should we create only one master build directory or one for each lib?
  • Can we have an option in CMakeLists.txt for STATIC vs. SHARED linking?
like image 492
Cartesius00 Avatar asked Dec 12 '11 12:12

Cartesius00


People also ask

What is the CMakeLists txt file?

CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.

How do I create a project using CMakeLists txt?

In Qt Creator, go to File → Open File or Project… and choose CMakeLists. txt from the source folder you want to build. Qt Creator will prompt you for the location of the binary folder, calling it the “build directory”. By default, it suggests a path adjacent to the source folder.


1 Answers

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:

  1. 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.

  2. 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:

project( P ) # Setup lib1  set ( LIB1_SOURCES ... ) # Fill in your set of source-files here... add_library( lib1 ${LIB1_SOURCES} ) # Do similar for lib2 and lib3 target_link_libraries( lib2 lib1 ) # Indicate that lib1 is needed for lib2 target_link_libraries( lib3 lib1 lib2 ) # Indicate that lib2 and lib1 are needed for lib3 

Example 2:

project( P ) add_subdirectory( lib1 ) add_subdirectory( lib2 ) add_subdirectory( lib3 ) 

In each subdirectory you then write your CMakeLists.txt. E.g. in case of lib3:

project( lib3 ) set( LIB3_SOURCES ... ) # Setup the list of sources here. add_library( lib3 ${LIB3_SOURCES} ) # You can refer to other libraries which should be available from the root cmakelists. target_link_libraries( lib3 lib1 lib2 ) 
like image 150
André Avatar answered Oct 13 '22 01:10

André