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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:17:13+00:00 2026-05-27T18:17:13+00:00

I’ve just migrated a somewhat large project from Visual Studio solutions to CMake and

  • 0

I’ve just migrated a somewhat large project from Visual Studio solutions to CMake and I’ve noticed a weird behavior. I have something like the following structure:

project/CMakeLists.txt
project/code/CMakeLists.txt

project/code/library-1/CMakeLists.txt
project/code/library-1/*.hpp
project/code/library-1/*.cpp
project/code/library-2/CMakeLists.txt
project/code/library-2/*.hpp
project/code/library-2/*.cpp
...
project/code/library-n/CMakeLists.txt
project/code/library-n/*.hpp
project/code/library-n/*.cpp

project/demo/CMakeLists.txt
project/demo/demo-1/CMakeLists.txt
project/demo/demo-1/*.hpp
project/demo/demo-1/*.cpp
project/demo/demo-2/CMakeLists.txt
project/demo/demo-2/*.hpp
project/demo/demo-2/*.cpp
...
project/demo/demo-n/CMakeLists.txt
project/demo/demo-n/*.hpp
project/demo/demo-n/*.cpp
  1. The root CMakeLists.txt file configures the compilation flags, macro definitions, etc. and uses CMake’s add_subdirectory() to include targets defined by the libraries and demo projects.
  2. The code sub-folder contains a flat list of sub-folders with each containing source code for a static library (as well as its target defined in a CMakeLists.txt file).
  3. The demo sub-folder contains a flat list of sub-folders. Each contains source code for an executable and associated CMakeLists.txt file.
  4. Each library is a standalone component and builds independently from all other libraries and demo projects.
  5. Each demo program depends on one or more of the different libraries in the code sub-folder.

This setup is really nice. If I want to change build options, I only need to modify the root CMakeLists.txt and everything re-compiles with the new settings. If I modify any source code anywhere in the tree, the appropriate libraries, if any, are recompiled and all dependent demo programs are also re-built.

However, if I modify any CMakeLists.txt file anywhere in the tree, the entire tree of libraries and programs is re-compiled without respect of dependencies. To give an idea of what I mean, here a few parts of the CMake build scripts.


project/demo/CMakeLists.txt

# Resolve libraries built in `code` sub-folder.
link_directories(${LIBRARY_OUTPUT_PATH})

set(demo-projects
  demo-1
  demo-2
  ...
  demo-n
)
foreach(demo-project ${demo-projects})
  add_subdirectory(${demo-project})
endforeach()

project/demo/demo-n/CMakeLists.txt

# Find all source code in the same folder.
file(GLOB ${demo-project}_headers
  ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp
)
file(GLOB ${demo-project}_sources
  ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
)

# Select libraries to link with.
set(${demo-project}_libraries
  library-1
  library-2
  library-5
)

# Build the demo program.
add_executable(${demo-project}
  ${${demo-project}_headers}
  ${${demo-project}_sources}
)
if(${demo-project}_libraries)
  target_link_libraries(${demo-project} ${${demo-project}_libraries})
endif()

# Manually register some dependencies on other targets.
if(${demo-project}_dependencies)
  add_dependencies(${demo-project} ${${demo-project}_dependencies})
endif()

If I happen to modify project/demo/demo-n/CMakeLists.txt to add an extra library, like this:

set(${demo-project}_libraries
  library-1
  library-2
  library-5
  library-6
)

Then the entire source code for all libraries and demo programs in the project is re-compiled. Why is this so? Is there a better way to structure my scripts in order to avoid this?

  • 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-27T18:17:14+00:00Added an answer on May 27, 2026 at 6:17 pm

    It happens that my problem was cause by a totally unrelated issue. I applied Bill Hoffman’s suggestion and the modifying any “CMakeLists.txt” file in the project ended up modifying the CXX_FLAGS (C++ compiler flags) variable in all generated Makefiles.

    I traced that back to my root “CMakeLists.txt” file, which had something like the following:

    if(MSVC)
      # ...
      set(CMAKE_CXX_FLAGS_DEBUG
        "${CMAKE_CXX_FLAGS_DEBUG} /WX /wd4355" // depends on cached value.
        CACHE STRING "Debug compiler flags" FORCE)
      # ...
    endif()
    

    I changed it to the following.

    if(MSVC)
      # ...
      set(CMAKE_CXX_FLAGS_DEBUG
        "/DWIN32 /D_WINDOWS /WX /wd4355" // no longer depends on cached value.
        CACHE STRING "Debug compiler flags" FORCE)
      # ...
    endif()
    

    CMake no longer repeats the /WX /wd4355 flags when updating the build scripts and my project no longer re-compiles from scratch at each modification!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a bunch of posts stored in text files formatted in yaml/textile (from
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from

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.