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

  • Home
  • SEARCH
  • 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 8832555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:33:35+00:00 2026-06-14T08:33:35+00:00

I am new to CMake. I have got my project compiling. I have a

  • 0

I am new to CMake. I have got my project compiling. I have a structure like this

PROJECT

  • SRC

    1. test
      .cpp (all source files) and CMakeLists.txt for this folder (creating a static library)
    2. example
      .cpp (all source files) and CMakeLists.txt for this folder (creating a static library)
  • Include

    1. test
      .h (all the header files)
    2. example
      .h (all the header files)
  • build

    1. CMakeLists.txt (Main CMakelist file)
  • lib

    1. test (contains the generated files)
    2. example (contains the generated files)

The question is how do I copy all static files ie. from test and example folder and place them in a different folder outside the binary structure recursively?

My main CMakeLists.txt file:

PROJECT(copythefiles)

SET(CMAKE_CURRENT_BINARY_DIR ".")

add_subdirectory(/src/test /lib/test) # I am specifying the location where the files are to be generated
add_subdirectory(/src/example /lib/example)

ADD_CUSTOM_TARGET( a ALL COMMAND ${CMAKE_COMMAND} -E echo "\{X}" > ${CMAKE_CURRENT_BINARY_DIR}/lib/test/libtest.a )

ADD_CUSTOM_COMMAND(TARGET a POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${CMAKE_CURRENT_BINARY_DIR}/lib/test/libtest.a
        ${CMAKE_CURRENT_BINARY_DIR}/lib/libtest.a )

This copies the files. But I have around 20 projects and I would like to do it recursively.

Thanks 🙂

  • 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-14T08:33:36+00:00Added an answer on June 14, 2026 at 8:33 am

    It’s normal to have your top-level CMakeLists.txt file in the root of your project. If you move your CMakeLists.txt out of “/build” to the root, then you should be able to call add_subdirectory without having to specify the binary path for each case.

    Assuming you move the CMakeLists file, then you can insert this before the add_subdirectory calls:

    function(MoveLib TheTarget)
      add_custom_command(TARGET ${TheTarget} POST_BUILD
                         COMMAND ${CMAKE_COMMAND} -E
                             copy $<TARGET_FILE:${TheTarget}> ${CMAKE_SOURCE_DIR}/lib)
    endfunction()
    
    execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_SOURCE_DIR}/lib)
    

    This then allows you to add e.g. MoveLib(my_test) inside the libraries’ CMakeLists.txt files, where my_test is the name of the library concerned.

    A copy of all libraries will then end up in “/lib”. If you’re not really wanting copies, then you should have a look at the ARCHIVE_OUTPUT_DIRECTORY property. If you simply add

    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
    

    then all your static libraries will end up in “/lib”. There are a couple of things to watch for here though.

    Shared libraries aren’t covered by ARCHIVE_OUTPUT_DIRECTORY. The details for shared libs are in the docs for ARCHIVE_OUTPUT_DIRECTORY though.

    Also, some generators (e.g. MSVC) append a per-configuration subdirectory to the specified directory, so you’d end up with “/lib/Debug”, “/lib/Release”, etc. You can circumvent this by setting the configuration-specific versions of CMAKE_ARCHIVE_OUTPUT_DIRECTORY to all point to ${CMAKE_SOURCE_DIR}/lib:

    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/lib)
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/lib)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to try CMake to manage a new C++ project. However, some files
I'm very new to CMake but have experience with general build environments like Make.
New to PHP and MySQL, have heard amazing things about this website from Leo
I'm new to cmake and I'm trying to install .hpp files while preserving directory
I'd like to use CMake to run some tests for an xslt coding project.
I've moved files from Project to Project/src . Git is under the impression that
I am not new to Django (1y work experience) but I have got a
I'm new to CMake and have trouble understanding some usage concepts. I'm calling a
I'm new to cmake and have just started playing around with it. My problem
I have a Cmake project where I use static libraries from another project (which

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.