I’m using cmake for my project. No I want to split some parts into a library and use this for 2 different applications.
Now I don’t how how to do this subprojects in cmake. My first attempt was to use the add_subdirectory command:
cmake_minimum_required(VERSION 2.8)
add_subdirectory(MSI)
message("Building MsiQtWizard with: ${MSI_INCLUDE_DIR}")
add_subdirectory(MsiQtWizard)
So MSI would be my library. Inside the MSI folder is another cmakelists which is basically a standalone list for building the library. I thought I could make the MsiQtWizard project also a standalone cmakelists, so I could theoretically build MSI and use the library to build MsiQtWizard (and other projects).
The cmakelists in the root directory would just be a helper to build the library and the GUI in one single step.
The problem is, for building MsiQtWizard, I need the include path to msi and the static library binaries. I tried to do something like that at the end of MIS/CMakelists.txt:
### Set variables, other scripts may use ###
SET(MSI_INCLUDE_DIR include)
MESSAGE("Include directory is: ${MSI_INCLUDE_DIR}")
and in the MsiQtWizard/CMakelists:
##### external libraries #####
#MSI
find_path(MSI_INCLUDE_DIR REQUIRED msi/Image.hpp
PATH_SUFFIXES MSI/include include)
My intend is, that MsiQtWizard will search for msi, if the varaible was not previously set (so that you could use this cmakelists as a standalone). When building MSI, I want to save the include path (and later binary locations) and pass it to MsiQtWizard – but the value is gone as soon as I’m back in my root cmakelists.
So that is, what I tried. My Question is now: How would I correctly split my project into a library and a (later multiple) application and can I do it in a way that I can also build them independently?
Or, more specific: How can I pass values from a node CMakelist to the root CMakeList (like I tried with MSI_INCLUDE_DIR)?
If your building a library – its best to completely separate it from the application build. Otherwise you are coupling your library with your application with
cmake, which in my view defeats the purpose of building a library.When building your library you will want something like
where
srccontains your library code. You can thenmakethensudo make installyour library to your standard library location (e.g. /usr/lib).You can then use your library in any subsequent project. Put these in a new directory and create a new
CMakeLists.txtfor them.You will want something like,
Now all you need to do is help
cmakefind you library.You can include a module for this. In the file
./cmake/Modules/FindMSILibrary.cmaketype something like:That should be it.
EDIT:
If you really want to package your applications with your library (perhaps some example applications), then you can do something like so:
In your root CMakeLists.txt
Your library
MSI-src/CMakeFiles.txtwill be as before, and yourexample/MSI-project-1/CMakeLists.txtwill be something like