Home > front end >  How can I write a CMakeList.txt file for heavily nested project?
How can I write a CMakeList.txt file for heavily nested project?

Time:01-19

Suppose my source code has the following directory structure:

C:\USERS\PC\SOURCE\REPOS\my_app_src
├───apps
│   └───biosimulations
├───core
│   ├───algorithms
│   │   └───trees
│   ├───alignment
│   │   ├───scoring
│   │   └───tasks
│   ├───calc
│   │   ├───numeric
│   │   ├───statistics
│   │   └───structural
│   │       └───transformations
│   ├───chemical
│   └───data
│       ├───basic
│       ├───io
│       ├───sequence
│       └───structural
│           └───selectors
├───simulations
│   ├───evaluators
│   │   └───cartesian
│   ├───forcefields
│   │   ├───cabs
│   │   ├───cartesian
│   │   └───mf
│   │   
│   ├───movers
│   ├───observers
│   │   └───cartesian
│   ├───sampling
│   └───systems
│       
├───ui
│   └───tasks
└───utils
    ├───exceptions
    └───options

Each folder has any number of header and source files with any name.

How can I write a CMakeList.txt file for this project?

CodePudding user response:

Using add_subdirectory and add_library commands, recursively add all of the source files in the directory structure.

cmake_minimum_required(VERSION 3.x)
project(my_app)

# Add subdirectories recursively
add_subdirectory(apps)
add_subdirectory(core)
add_subdirectory(simulations)
add_subdirectory(ui)
add_subdirectory(utils)

# Create the final executable
add_executable(my_app main.cpp)

# Link the libraries to the executable
target_link_libraries(my_app core simulations apps ui utils)

Then in each subdirectories(apps,core,simulations,ui, utils) you would need to add a new CMakeLists.txt that tells which source files are in that directory and create a library.

cmake_minimum_required(VERSION 3.x)

SET(SRC_FILES 
    program.cpp
    )

#for static libraries
add_library(core STATIC ${SRC_FILES})

#for dynamically linking libraries
add_library(core SHARED ${SRC_FILES})

# If you need executable here
# add_executable(core ${SRC_FILES})

This needs to be repeated for all subdirectories, untill all sources are covered. Above examples gives general structure, you need to define CMAKE flags as required.

  • Related