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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:11:31+00:00 2026-06-16T13:11:31+00:00

I currently have the following architecture `– system |– CMakeLists.txt |– dll | |–

  • 0

I currently have the following architecture

`-- system
    |-- CMakeLists.txt
    |-- dll
    |   |-- CMakeLists.txt
    |   |-- UnixDLoader.cpp
    |   |-- ...
    |   `-- WinDLoader.hh
    |-- sockets
    |   |-- CMakeLists.txt
    |   |-- crossplateform_utils.h
    |   |-- ...
    |   `-- Udp.hh
    `-- threads
        |-- CMakeLists.txt
        |-- IMutex.hh
        |-- ...
        `-- WinThread.hh

My root CMakeLists.txt is as follow

project(system)

add_subdirectory(dll)
add_subdirectory(sockets)
add_subdirectory(threads)

add_library(${PROJECT_NAME}
    ${${PROJECT_NAME}_HEADERS}
    ${${PROJECT_NAME}_SOURCES}
)

and in threads/ for example, I have the following

list(APPEND ${PROJECT_NAME}_HEADERS
    IThread.hh
    UnixThread.hh
    WinThread.hh
    IMutex.hh
    UnixMutex.hh
    WinMutex.hh
)
set(${PROJECT_NAME}_HEADERS ${${PROJECT_NAME}_HEADERS} PARENT_SCOPE)

list(APPEND ${PROJECT_NAME}_SOURCES
    UnixThread.cpp
    WinThread.cpp      
    UnixMutex.cpp
    WinMutex.cpp    
    PARENT_SCOPE
)
set(${PROJECT_NAME}_SOURCES ${${PROJECT_NAME}_SOURCES} PARENT_SCOPE)

if(UNIX)
    target_link_libraries(${PROJECT_NAME} pthread)
endif(UNIX)

but, as add_subdirectory() creates a new scope, my ${PROJECT_NAME}_* variables are empty. I read about set(... PARENT_SCOPE), but the paths become invalids and anyhow cmake produces an error on the call to target_link_libraries() in system/threads/CMakeLists.txt saying Cannot specify link libraries for target "system" which is not built by this project.. Scope issue again.

So I though I would be nice if I found a solution to bypass the scope creation, but I’ll take any solution that keeps the logic of my architecture.

  • 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-06-16T13:11:32+00:00Added an answer on June 16, 2026 at 1:11 pm

    You have a couple of reasonable options here I think.

    The add_subdirectory command is often used to include a directory (it needn’t actually be a subdirectory in the filesystem sense) which really contains a standalone module, e.g. a library or executable; one which can be built without the help of a parent CMakeLists file. In this case, the submodule’s CMakeLists.txt would contain its own project command, rendering the use of a global ${PROJECT_NAME} variable difficult.

    In your case, it appears that you simply want the subdirectories’ CMakeLists files to append lists of files to variables defined in the parent scope. You can achieve this by making use of the CMake variable CMAKE_CURRENT_LIST_DIR in your subordinate CMakeLists:

    set(${PROJECT_NAME}_HEADERS
        ${${PROJECT_NAME}_HEADERS}
        ${CMAKE_CURRENT_LIST_DIR}/IThread.hh
        ${CMAKE_CURRENT_LIST_DIR}/UnixThread.hh
        ${CMAKE_CURRENT_LIST_DIR}/WinThread.hh
        ${CMAKE_CURRENT_LIST_DIR}/IMutex.hh
        ${CMAKE_CURRENT_LIST_DIR}/UnixMutex.hh
        ${CMAKE_CURRENT_LIST_DIR}/WinMutex.hh
        PARENT_SCOPE
    )
    
    set(${PROJECT_NAME}_SOURCES
        ${${PROJECT_NAME}_SOURCES}
        ${CMAKE_CURRENT_LIST_DIR}/UnixThread.cpp
        ${CMAKE_CURRENT_LIST_DIR}/WinThread.cpp      
        ${CMAKE_CURRENT_LIST_DIR}/UnixMutex.cpp
        ${CMAKE_CURRENT_LIST_DIR}/WinMutex.cpp    
        PARENT_SCOPE
    )
    

    Ensure you don’t have a project command in your subdirectories’ CMakeLists files.

    This can be made slightly simpler in this case by swapping the add_subdirectory command with the include command, hence avoiding scoping issues.

    To do this, remove the PARENT_SCOPE args from the set commands, and in the top-level CMakeLists.txt:

    add_subdirectory(dll)
    add_subdirectory(sockets)
    add_subdirectory(threads)
    include(dll/CMakeLists.txt)
    include(sockets/CMakeLists.txt)
    include(threads/CMakeLists.txt)

    Your other issue relating to target_link_libraries(${PROJECT_NAME} pthread) is simply down to the fact that the add_library command defining the library ${PROJECT_NAME} is called after the target_link_libraries command. Your simplest option is probably to move the target_link_libraries command to the parent CMakeLists.txt

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

Sidebar

Related Questions

For the web application (ASP.NET MVC) I'm currently developing, we have the following architecture
Currently I have following XML: <Rowsets> <Rowset> <Row> <DrilldownDepth>0</DrilldownDepth> <ScheduleWeek>---</ScheduleWeek> <ABC>---</ABC> <PQR>1500</PQR> <XYZ>15000</XYZ> <Quant>285</Quant>
I currently have the following controller method in a Rails app: def index @entries
I currently have the following SQL query that lists all the names of all
I currently have the following Get-ChildItem -recurse -include file.ext | Select-String -pattern c: |
I currently have the following htaccess rewrite rule: RewriteRule (.*) index.php/$1 [L] How do
I currently have the following CSS (called splash.css): html { background:url(splash.jpg) center no-repeat; background-size:auto
I currently have the following code which delays showing a fixed bar after a
I currently have the following in select expert to select a bunch of fields:
I currently have the following CSS for my 4 columns which might grow to

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.