# Build explicit target with ExternalProject

**URL:** <https://discourse.cmake.org/t/build-explicit-target-with-externalproject/442>\
**Category:** Code\
**Created:** [December 30, 2019, 11:46am UTC](https://discourse.cmake.org/t/build-explicit-target-with-externalproject/442 "2019-12-30T11:46:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MBZ](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mbz/32/268_2.png) [@MBZ](https://discourse.cmake.org/u/MBZ)\
**Post date:** [December 30, 2019, 11:46am UTC](https://discourse.cmake.org/t/build-explicit-target-with-externalproject/442/1 "2019-12-30T11:46:06Z")

</div>

How do I build specific target with ExternalProject ?  
scenario 1: my external project has few targets and I would like to build only on of them  
scenario 2: the targets in my external project has EXCLUDE\_FROM\_ALL so I must specify the required target other wise it will not be built.

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [December 30, 2019, 1:22pm UTC](https://discourse.cmake.org/t/build-explicit-target-with-externalproject/442/2 "2019-12-30T13:22:03Z")

</div>

By using commands `ExternalProject_Add_Step` and `ExternalProject_Add_StepTargets` (see [Explicit Step Management](https://cmake.org/cmake/help/latest/module/ExternalProject.html#explicit-step-management)), you can launch easily any target from your external project.

---

<div class="post-metadata">

**Author:** ![MBZ](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mbz/32/268_2.png) [@MBZ](https://discourse.cmake.org/u/MBZ)\
**Post date:** [December 30, 2019, 1:30pm UTC](https://discourse.cmake.org/t/build-explicit-target-with-externalproject/442/3 "2019-12-30T13:30:55Z")

</div>

Thank you,  
I have read this documentation but I was not able to use it successfully, probably I’m missing something:

lets say I have ExternalProject named _foo_ and it has a target named **bar** created with EXCLUDE\_FROM\_ALL.

can you please provide the instructions for building the **bar** target ?

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [December 30, 2019, 2:08pm UTC](https://discourse.cmake.org/t/build-explicit-target-with-externalproject/442/4 "2019-12-30T14:08:45Z")

</div>

Assuming, you have an external project based on `CMake`, here is a minimal example (for a more realistic declaration, `DEPENDERS` and `DEPENDEES` should be considered):

```cmake
ExternalProject_Add_Step(foo bar
    COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target bar
    WORKING_DIRECTORY <BINARY_DIR>)

ExternalProject_Add_StepTargets(foo bar)

```

With these declarations, you can now use target `foo-bar` in your own project. Or even build directly this target by using the command `cmake --build <my-binary-dir> --target foo-bar`
