How to build external deps once and use multiple times?

Lets consider myself as newbie, First, I would say all these are working completely fine, Here everytime I build, all deps will compile again. So I want to build all deps and keep its built file(headers and libraries) in separate folder and use it through out compilation without using extenal deps. Now, I can able to build static and dynamically(Shared). I want to do the same, but as I said above, I want external deps built file to be separate folder. like I said “Build once and use multiple times”.

/home

mydir/
    project/
        build/
        CMakeLists.txt #toplevel or root level Cmake
        deps/
            curl-7.43.0/
            libiconv-1.14/
            libpng-1.6.18/
            libssh2-1.6.0/
            openssl-1.0.2d/
            sqlite/
            tinycthread/
            zlib-1.2.8/ 
            CMakeLists.txt #contains all deps as external_project_add
        src/
            foo-1/
                foo-1/
                     foo-1.hpp
                     foo-1.cpp
                test/
                     test.cpp
                CMakeLists.txt
            foo-2/
                foo-2/
                     foo-2.hpp
                     foo-2.cpp
                test/
                    test.cpp
                CMakeLists.txt #contains target link library and other 
             .
             .
             .
               n files
            CMakeLists.txt # contains all src file as subdirectiory_add(foo-1) and so on

Any leads will be appreciated.
P.S: I dont want install via package(linux). Since I want to run this in both windows and linux. I want to compile using CMake only.

I would suggest investigating something like a superbuild (using ExternalProject) or a more integrated solution using FetchContent depending on how flexible it needs to be.

Cc: @craig.scott

Hi @ben.boeckel and @craig.scott,

I used externalProject_Add for external deps. HereI I dont want superbuild. below is the one of the dependencies build and I don’t want to run this everytime.

if(HDR_ONLY)
    # Header-only
    add_subdirectory(${CMAKE_THIRD_PARTY_LIBS}/fmt-8.1.1 EXCLUDE_FROM_ALL)
    add_library(fmt_lib ALIAS fmt-header-only)
else()
    # Linkage: dynamic
    add_subdirectory(${CMAKE_THIRD_PARTY_LIBS}/fmt-8.1.1)
    add_library(fmt_lib ALIAS fmt)
endif()

Above code generates build file and built file will store in $CMAKE_BINARY_DIR, where I dont want to save it in CMAKE_BINARY_DIR, rather I need to save it in another folder called “/install/”. How to do that?

P.S: These kind of external deps libraries build using cmakefiles.txt(using add_subdirectory and add_subfolder). I just want to move its built file with required headers and libs to another folder? is it possible?