Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6852799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:26:08+00:00 2026-05-27T01:26:08+00:00

I’m using cmake for my project. No I want to split some parts into

  • 0

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)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:26:08+00:00Added an answer on May 27, 2026 at 1:26 am

    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

    project (MSILibrary)
    ADD_LIBRARY(MSILibrary  src/MSI1.cpp src/MSI2.cpp)
    install (TARGETS MSILibrary DESTINATION lib)
    

    where src contains your library code. You can then make then sudo make install your 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.txt for them.

    You will want something like,

    #include find modules
    set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
    
    project (MSI-project-1)
    
    find_package(MSILibrary REQUIRED)
    IF(MSILibrary_FOUND)
      include_directories(${MSILibrary_INCLUDE_DIRS} 
    ENDIF(MSILibrary_FOUND )
    
    
    target_link_libraries (MSI-project-1 ${MSILibrary_LIBRARIES})
    install (TARGETS MSI-project-1 DESTINATION bin)
    

    Now all you need to do is help cmake find you library.
    You can include a module for this. In the file ./cmake/Modules/FindMSILibrary.cmake type something like:

    # - Try to find MSILibrary library
    # Once done, this will define
    #
    #  MSILibrary_FOUND - system has MSILibrary
    #  MSILibrary_INCLUDE_DIRS - the MSILibrary include directories
    #  MSILibrary_LIBRARIES - link these to use MSILibrary
    
    ## Google this script (I think its fairly standard, but was not bundled with my CMAKE)   - it helps find the macros.
    include(LibFindMacros)
    
    # Dependencies
    libfind_package(MSILibrary)
    
    # Use pkg-config to get hints about paths
    libfind_pkg_check_modules(MSILibrary_PKGCONF MSILibrary)
    
    # Include dir
    find_path(MSILibrary_INCLUDE_DIR
      NAMES MSI.hpp
      PATHS ${MSI_Library_PKGCONF_INCLUDE_DIRS} 
    )
    
    # Finally the library itself
    find_library(MSILibrary_LIBRARY
      NAMES MSILibrary 
      PATHS ${MSILibrary_PKGCONF_LIBRARY_DIRS} 
    )
    
    # Set the include dir variables and the libraries and let libfind_process do the rest.
    # NOTE: Singular variables for this library, plural for libraries this this lib depends on.
    set(MSILibrary_PROCESS_INCLUDES MSILibrary_INCLUDE_DIR MSILibrary_INCLUDE_DIRS)
    set(MSILibrary_PROCESS_LIBS MSILibrary_LIBRARY MSILibrary_LIBRARIES)
    libfind_process(MSILibrary)
    

    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

    cmake_minimum_required (VERSION 2.6)
    project (MSIProject)
    
    # The version number.
    set (MSIProject_VERSION_MAJOR 0)
    set (MSIProject_VERSION_MINOR 1)
    set (MSIProject_PATCH_LEVEL 3 )
    
    # project options
    OPTION( BUILD_SHARED_LIBS "Set to OFF to build static libraries" ON )
    OPTION( BUILD_EXAMPLES "Set to OFF to skip building the examples" ON )
    
    # Put the libaries and binaries that get built into directories at the
    # top of the build tree rather than in hard-to-find leaf
    # directories.
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
    
    
    ##########################################################################
    #  Build the library 
    ##########################################################################
    
    add_subdirectory(MSI-src)
    
    ##################
    #  Build your example Apps if requested
    ############
    
    
    IF( BUILD_EXAMPLES )
      add_subdirectory(example/MSI-project-1)
      add_subdirectory(example/MSI-project-2)
    ENDIF( BUILD_EXAMPLES )
    

    Your library MSI-src/CMakeFiles.txt will be as before, and your example/MSI-project-1/CMakeLists.txt will be something like

    ## Make the InferData example project
    project (MSI-project-1)
    
    #include MSI library
    include_directories ("${MSILibrary_SOURCE_DIR}/include")
    #include the includes of this project
    include_directories ("${MSI-project-1_SOURCE_DIR}/../include")
    
    #build
    add_executable(MSI-project-1  src/P1.cpp)
    target_link_libraries (MSI-project-1 MSILibrary) #link
    
    install (TARGETS MSI-project-1 DESTINATION bin)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.