# Is it possible to add\_subdirectory() after another target has finished installing ( configure second subdirectory after first subdirectory is finished installing )

**URL:** <https://discourse.cmake.org/t/is-it-possible-to-add-subdirectory-after-another-target-has-finished-installing-configure-second-subdirectory-after-first-subdirectory-is-finished-installing/13217>\
**Category:** Code\
**Created:** [December 17, 2024, 10:11pm UTC](https://discourse.cmake.org/t/is-it-possible-to-add-subdirectory-after-another-target-has-finished-installing-configure-second-subdirectory-after-first-subdirectory-is-finished-installing/13217 "2024-12-17T22:11:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![n22](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/n/c57346/32.png) [@n22](https://discourse.cmake.org/u/n22)\
**Post date:** [December 17, 2024, 10:11pm UTC](https://discourse.cmake.org/t/is-it-possible-to-add-subdirectory-after-another-target-has-finished-installing-configure-second-subdirectory-after-first-subdirectory-is-finished-installing/13217/1 "2024-12-17T22:11:13Z")

</div>

I tried add\_custom\_target with POST\_BUILD and manually installing it there then adding a dependency to the library build by the second subdirectory on that target but it doesn’t do it and i can’t force it to not be configured until the first subdirectory is finished installing.

in the configuring/generating of the second subdirectory i need the executable from the first subdirectory to compile things in the second and also for find\_package so configuring gives errors and i cant make it wait for installation. i’m not sure if there’s a way to make the add\_subdirectory() wait for that. I tried externalproject but linking the libraries to my target doesn’t work for some reason and alot of variables don’t get set when passing them as -D in the externalproject\_add() im not sure why.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [December 18, 2024, 1:07pm UTC](https://discourse.cmake.org/t/is-it-possible-to-add-subdirectory-after-another-target-has-finished-installing-configure-second-subdirectory-after-first-subdirectory-is-finished-installing/13217/2 "2024-12-18T13:07:44Z")

</div>

Yes, it is i.e.:

```cmake
# test if the targets are usable from the install directory
add_test(
    NAME find-package-test
    COMMAND
        ${CMAKE_CTEST_COMMAND}
        # --verbose
        --output-on-failure -C $<CONFIG> --build-and-test #
        "${CMAKE_CURRENT_SOURCE_DIR}/test-FindOpenSSL" "${CMAKE_CURRENT_BINARY_DIR}/find-package-test" #
        --build-generator ${CMAKE_GENERATOR} --build-makeprogram ${CMAKE_MAKE_PROGRAM} #
        --build-options #
        "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}" #
        "-DCMAKE_C_STANDARD=${CMAKE_C_STANDARD}" #
        "-DCMAKE_BUILD_TYPE=$<CONFIG>" #
        "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" #
        "-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}"
)

```

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [December 18, 2024, 1:14pm UTC](https://discourse.cmake.org/t/is-it-possible-to-add-subdirectory-after-another-target-has-finished-installing-configure-second-subdirectory-after-first-subdirectory-is-finished-installing/13217/3 "2024-12-18T13:14:05Z")

</div>

Or as a post build command for target `install` like this:

```cmake
if(ZLIB_DEBUG_POSTFIX)
    add_custom_command(
        TARGET openssl
        POST_BUILD
        COMMAND
            ${CMAKE_COMMAND} -E copy_if_different libssl${_CMAKE_LINK_LIBRARY_SUFFIX}
            libssl${ZLIB_DEBUG_POSTFIX}${_CMAKE_LINK_LIBRARY_SUFFIX}
        COMMAND
            ${CMAKE_COMMAND} -E copy_if_different libcrypto${_CMAKE_LINK_LIBRARY_SUFFIX}
            libcrypto${ZLIB_DEBUG_POSTFIX}${_CMAKE_LINK_LIBRARY_SUFFIX}
        VERBATIM
        WORKING_DIRECTORY ${OPENSSL_LIB_DIR}
    )
endif()

```

---

<div class="post-metadata">

**Author:** ![n22](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/n/c57346/32.png) [@n22](https://discourse.cmake.org/u/n22)\
**Post date:** [December 19, 2024, 10:16am UTC](https://discourse.cmake.org/t/is-it-possible-to-add-subdirectory-after-another-target-has-finished-installing-configure-second-subdirectory-after-first-subdirectory-is-finished-installing/13217/4 "2024-12-19T10:16:18Z")

</div>

but how do i add\_subdirectory() after the installation is done?

---

<div class="post-metadata">

**Author:** ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)\
**Post date:** [December 19, 2024, 10:27am UTC](https://discourse.cmake.org/t/is-it-possible-to-add-subdirectory-after-another-target-has-finished-installing-configure-second-subdirectory-after-first-subdirectory-is-finished-installing/13217/5 "2024-12-19T10:27:58Z")

</div>

Not sure what you mean. `add_subdirectory()` happens at configure time (ie before the build), and installation happens after configure & build.

---

<div class="post-metadata">

**Author:** ![n22](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/n/c57346/32.png) [@n22](https://discourse.cmake.org/u/n22)\
**Post date:** [December 19, 2024, 11:05am UTC](https://discourse.cmake.org/t/is-it-possible-to-add-subdirectory-after-another-target-has-finished-installing-configure-second-subdirectory-after-first-subdirectory-is-finished-installing/13217/6 "2024-12-19T11:05:58Z")

</div>

sorry for not properly explaining,  
i have 2 subdirectories, 1 builds an executable needed by the second subdirectory so i want to configure the second subdirectory after the first subdirectory is installed so it can find the executable to use in configuration/generation.  
im trying to make the question as general as possible but if it helps it’s protobuf and the executable is protoc

right now i have:

```auto
add_subdirectory(./proto)
add_custom_target(install_proto
    COMMAND ${CMAKE_COMMAND} -P instproto.cmake
    DEPENDS libprotobuf
)
add_custom_target(link_gns
    COMMAND ${CMAKE_COMMAND} -P addgns.cmake
    DEPENDS install_proto
)
add_dependancies(main linkgns)

instproto.cmake:include("./proto/cmake_install.cmake")
addgns.cmake:add_subdirectory(./gns)

```

but still it doesn’t wait

---

<div class="post-metadata">

**Author:** ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)\
**Post date:** [December 19, 2024, 11:08am UTC](https://discourse.cmake.org/t/is-it-possible-to-add-subdirectory-after-another-target-has-finished-installing-configure-second-subdirectory-after-first-subdirectory-is-finished-installing/13217/7 "2024-12-19T11:08:12Z")

</div>

You cannot `add_subdirectory` from a script or a custom target. The way to solve this is to make the build/install of your libprotobuf execute during configure of the main project, such as via `execute_process`

---

<div class="post-metadata">

**Author:** ![n22](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/n/c57346/32.png) [@n22](https://discourse.cmake.org/u/n22)\
**Post date:** [December 20, 2024, 11:45am UTC](https://discourse.cmake.org/t/is-it-possible-to-add-subdirectory-after-another-target-has-finished-installing-configure-second-subdirectory-after-first-subdirectory-is-finished-installing/13217/8 "2024-12-20T11:45:52Z")

</div>

thank you, `execute process` works every time. i also want to add `ExternalProject` works too but sometimes variables don’t get set when they are passed to the externalproject through `CMAKE_ARGS` without changing any files and i don’t know of a way to force variables like i would with `set( [FORCE])` but for external project so im not sure why that happens.
