Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add a library to a project so that if the library changes, it (the lib) gets recompiled first before the program in CMake?

I’m developing a simple library and in the past I’ve been using Premake. Now, I want to change that and use CMake. I’ve been struggling with ‘porting’ my workflow to CMake.

I want to have 2 projects, one for the library and one for the testing program. The library should not be dependent on the testing program but the testing program should include the library and be dependent on it.
For example, if I change something in the library and compile my testing program, I want the library to recompile and then the testing program, so that it uses the latest library version.

I was able to do that in Premake but I just can’t seem to figure it out yet in CMake.
From my understanding, there should a top level CMake file and then the library should have one and the program should have one, just like I did in Premake.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Currently I only managed to create my library CMake file, but nothing else. A little help would be appreciated, especially as I’m noob in CMake.

My project structure looks like this

Root
├───Application
│   ├───Assets/
│   └───src/
│       └───Scenes/
├───Library
    ├───BUILD/
    ├───include/
    └───src/
    └───CMakeLists.txt

>Solution :

Have just one project with a single CMakeLists.txt in the root, something like this

cmake_minimum_required( VERSION 3.0 )
project( MainProject )
add_subdirectory( Library )
add_subdirectory( Application )

Once you add the Library as a dependency of Application it all should work.
Example:

# in Library/CMakeLists.txt
add_library( mylib file1.cpp file2.cpp )

Then in the app

# in Application/CMakeLists.txt
add_executable( myapp main.cpp )
target_link_libraries( myapp mylib )

that should recompile both mylib and relink myapp as soon as either file1.cpp or file2.cpp change.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading