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 6956535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:52:56+00:00 2026-05-27T14:52:56+00:00

We have a project P ( C/C++ on Linux) consisting of libraries lib1 ,

  • 0

We have a project P (C/C++ on Linux) consisting of libraries lib1, lib2, lib3.

  • lib1 is standalone linked to another system-wide libs
  • lib2 links to lib1
  • lib3 links to both lib1 and lib2

We have a directory P and extra directories for each of our libs (so, P/lib1/, P/lib2/…). Every library has also its own tests.

Questions:

  • Please, how to organize CMakeLists.txt for this scenario?
  • Should we create only one master build directory or one for each lib?
  • Can we have an option in CMakeLists.txt for STATIC vs. SHARED linking?
  • 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-27T14:52:57+00:00Added an answer on May 27, 2026 at 2:52 pm

    In this case, I would recommend using a single build/ directory. CMake will likely generate separate lib1, lib2 and lib3 directories in there.

    Switching between STATIC vs. SHARED can be done by using the BUILD_SHARED_LIBS flag (check the add_library documentation)

    With respect to the CMakeLists.txt organization, the choice is yours:

    1. You can build a single CMakeLists.txt which has multiple add_library entries.
      This has the benefit that you will get a single CMakeLists.txt, which some people may prefer when the projects are simple.

    2. You could split up your project into multiple CMakeLists.txt distributed over your lib1, lib2 and lib3 directories and use a root cmakelists.txt with add_subdirectory. The benefit of this setup is that it will be easier to generate the build-files with one call (in your build/ directory), but you could then easily step into e.g. lib3/ and call make/msbuild there. CMake will ensure that the dependencies are built correctly

    Example 1:

    project( P )
    # Setup lib1 
    set ( LIB1_SOURCES ... ) # Fill in your set of source-files here...
    add_library( lib1 ${LIB1_SOURCES} )
    # Do similar for lib2 and lib3
    target_link_libraries( lib2 lib1 ) # Indicate that lib1 is needed for lib2
    target_link_libraries( lib3 lib1 lib2 ) # Indicate that lib2 and lib1 are needed for lib3
    

    Example 2:

    project( P )
    add_subdirectory( lib1 )
    add_subdirectory( lib2 )
    add_subdirectory( lib3 )
    

    In each subdirectory you then write your CMakeLists.txt. E.g. in case of lib3:

    project( lib3 )
    set( LIB3_SOURCES ... ) # Setup the list of sources here.
    add_library( lib3 ${LIB3_SOURCES} )
    # You can refer to other libraries which should be available from the root cmakelists.
    target_link_libraries( lib3 lib1 lib2 )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I do have a simple cmake project (on linux) that loads some libraries from
I have a project where there are Windows, FreeBSD and Linux servers. I have
I'm compiling a NAnt project on linux with TeamCity Continuous Integration server. I have
I have an existing C++ project on a linux environment, and would like to
I have a django project that runs on a Linux server, and I've been
I have a large GUI project that I'd like to port to Linux. What
I am making a project that should compile on Windows and Linux. I have
I have a project for Linux that i've been developing for some time. Basically
I have a newbie Linux programming question. Suppose I have a project that uses
I have a project whose artifacts are two dynamic libraries, let's say libX.dylib and

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.