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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:07:59+00:00 2026-05-26T04:07:59+00:00

We would like to organize a C++ project like this: project/ lib1/ (first library)

  • 0

We would like to organize a C++ project like this:

project/
    lib1/       (first library)
        CMakeList.txt
        src/
            lib1.c
            foo1.h
        build/
        test/       (tests)
            CMakeList.txt
            test1.c
            test2.c
    lib2/       (second library)
        CMakeList.txt
        src/
            CMakeList.txt
            os/              (OS dependent code)
                CMakeList.txt
                win32/
                    xxx.c    (win32 implementation)
                linux/
                    xxx.c    (linux implementation)
            lib2.c
            foo2.h
        build/
    include/    (shared/public headers)
        lib1/
            lib.h    (shared library header included from apps)
        lib2/
            lib.h    (shared library header -"-)

Please, how to write those CMakeLists.txt when even lib2 should use link1 and when e.g. lib2 should be portable (at least Win32, Linux…)?

Correction: If some CMakeList.txt files are not on their places, please assume so. I probably forgot.

  • 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-26T04:07:59+00:00Added an answer on May 26, 2026 at 4:07 am

    The whole philosophy is to start with a central CMakeLists.txt for your whole project. At this level all the targets (libs, executables) are gonna be aggregated so there will be no problem linking from lib1 to lib2 for example. If lib2 is gonna be linking to lib1, lib1 needs to be built first.

    Platform specific source files should be set conditionally to some variable.
    (If you need to set variable in a subdirectory and use it in a directory above, you have to set it to the cache, using CACHE FORCE etc. – see manual for set)

    This is how you do proper out of source build – as CMake intends:

    cd project-build
    cmake ../project
    

    Having separate build directories per library is not very CMake’ish (if I may say so) and would probably require
    some hacks.

    project-build/
    project/
        CMakeLists.txt (whole project CMakeLists.txt)
        [
            project(MyAwesomeProject)
    
            include_directories(include) # allow lib1 and lib2 to include lib1/lib.h and lib2/lib.h
            add_subdirectory(lib1) # this adds target lib1
            add_subdirectory(lib2) # this adds target lib2
    
        ]
    
        lib1/       (first library)
            CMakeList.txt
            [
                add_library(lib1...)
                add_subdirectory(test)
            ]
            src/
                lib1.c
                foo1.h
            test/       (tests)
                CMakeList.txt
                test1.c
                test2.c
        lib2/       (second library)
            CMakeList.txt
            [
                add_subdirectory(src)
            ]
            src/
                CMakeList.txt
                [
                    if(WIN32)
                        set(lib2_os_sources os/win32/xxx.c)
                    elsif(LINUX)
                        set(lib2_os_sources os/linux/xxx.c)
                    else()
                        message(FATAL_ERROR "Unsupported OS")
                    endif()
                    add_library(lib2 SHARED lib2.c ${lib2_os_sources})
                ]
                os/              (OS dependent code)
                    win32/
                        xxx.c    (win32 implementation)
                    linux/
                        xxx.c    (linux implementation)
                lib2.c
                foo2.h
        include/    (shared/public headers)
            lib1/
                lib.h    (shared library header included from apps)
            lib2/
                lib.h    (shared library header -"-)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know what's the best way to organize my php project
I have an archive of Dilbert cartoons, and would like to organize them in
I would like to test a string containing a path to a file for
I have a django project with 2 apps like this: ## tags app, models.py
I would like to write a documentation for one of my Django based project.
I am using Jython to move images around and organise them. I would like
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
Would like to be able to set colors of headings and such, different font
Would like to know what a programmer should know to become a good at

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.