# How to build external deps once and use multiple times?

**URL:** https://discourse.cmake.org/t/how-to-build-external-deps-once-and-use-multiple-times/5553
**Category:** Code
**Tags:** os:linux, os:windows, comp:msvc
**Created:** [April 27, 2022, 8:54am UTC](https://discourse.cmake.org/t/how-to-build-external-deps-once-and-use-multiple-times/5553 "2022-04-27T08:54:53Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Iamniki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/i/58f4c7/32.png) [@Iamniki](https://discourse.cmake.org/u/Iamniki)
#### Post date: [April 27, 2022, 8:54am UTC](https://discourse.cmake.org/t/how-to-build-external-deps-once-and-use-multiple-times/5553/1 "2022-04-27T08:54:53Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 27, 2022, 1:32pm UTC](https://discourse.cmake.org/t/how-to-build-external-deps-once-and-use-multiple-times/5553/2 "2022-04-27T13:32:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Iamniki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/i/58f4c7/32.png) [@Iamniki](https://discourse.cmake.org/u/Iamniki)
#### Post date: [April 28, 2022, 8:43am UTC](https://discourse.cmake.org/t/how-to-build-external-deps-once-and-use-multiple-times/5553/3 "2022-04-28T08:43:37Z")

</div>

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.

```cmake
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?
