1
votes

I am building a project which contains lots of sub projects. for example as ...

LibA (build as shared library)

LibB (depends on LibA & build as shared library)

AppB (depends on LinB)

Directory structure (Which I want) is as ...

bin/
output/
src/libA/
src/libB/
src/appB/

Each of sub projects (LibA, LibB & AppB) has their own CMakeLists.txt file.

I want ..

1. Build LibA as shared library (I know how to do it)

2. Build LibB as shared library with linking of LibA (Don't Know how to do)
   Explanation: When I start building LibB, 
                LibA build first and 
                ready to link for LibB 
                when LibB ready to finish

3. Build AppB : If I start building AppB, 
                LibA build first and 
                LibB build after and
                both linked to AppB

Now I know classic way, build separately LibA & LibB and supplied path of lib and include to AppB. But I want to build them at once like

  Build LibA 
  Build LibB (if LibA is already build then ignore, else build LibA)
  Build AppB (if LibA, LibB are already build then ignore, else build them)

What I want

  • How can I achieve such behavior using CMAKE ?
  • It should be cross platform
  • Simple enough to include many more sub projects
1
One way is to add a CMakeLists.txt file at the top level directory to call add_subdirectory() on each of the projects you wish to pull in. I suggest you review this answer.squareskittles

1 Answers

2
votes

Here is one solution. You can use a top-level CMakeLists.txt file to tie all the projects together. So in your directory structure, this would be placed here:

bin/
output/
src/libA/
src/libB/
src/appB/
CMakeLists.txt    <--- Top-level CMakeLists.txt

Your top-level CMakeLists.txt file (as a sibling to the src directory) could look like this:

cmake_minimum_required(VERSION 3.11)

# Add your dependencies in the order you want them to build.
add_subdirectory(src/libA)
add_subdirectory(src/libB)
add_subdirectory(src/appB)

With each of the src directories having their own CMakeLists.txt file, here's an example of each of these individually.

You can set up LibA as a shared library with CMake with the CMakeLists.txt file in src/libA:

project(LibA_Project)
add_library(LibA SHARED sourceA.cpp  ... more sources here ...)

Next, CMake will traverse to the src/libB directory to configure LibB. Here is what the src/libB/CMakeLists.txt file could look like:

project(LibB_Project)
# Create a shared library for LibB as well.
add_library(LibB SHARED sourceB.cpp  ... more sources here ...)

# Link LibA to LibB as a dependency.
target_link_libraries(LibB LibA)

Finally, CMake will go to the src/appB directory. Here is that CMakeLists.txt file:

project(AppB_Project)
# Create the executable AppB.
add_executable(AppB main.cpp ... more sources here ...)

# Link LibA and LibB to AppB as dependencies.
target_link_libraries(AppB LibA LibB)

This approach can easily be expanded to include more sub-projects (e.g. LibC, LibD, AppE, etc) if necessary.