# Export third part library built with ExternalProject\_Add

**URL:** https://discourse.cmake.org/t/export-third-part-library-built-with-externalproject-add/10038
**Category:** Code
**Tags:** os:linux
**Created:** [February 8, 2024, 3:23pm UTC](https://discourse.cmake.org/t/export-third-part-library-built-with-externalproject-add/10038 "2024-02-08T15:23:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![KmakD](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kmakd/32/4289_2.png) [@KmakD](https://discourse.cmake.org/u/KmakD)
#### Post date: [February 8, 2024, 3:23pm UTC](https://discourse.cmake.org/t/export-third-part-library-built-with-externalproject-add/10038/1 "2024-02-08T15:23:20Z")

</div>

I need to install a library that my project depends on. To make my package more independent I moved the installation of it to cmake. I did it successfully with ExternalProject\_Add with a setup similar to the one shown below.

```cmake
include(ExternalProject)

file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/library-build/include)

ExternalProject_Add(
  library-build
  .
  .
  .
  )

add_library(library STATIC IMPORTED GLOBAL)
add_dependencies(library library-build)

set_target_properties(
  library
  PROPERTIES
    IMPORTED_LOCATION
    ${CMAKE_CURRENT_BINARY_DIR}/library-build/lib/library.so)
set_target_properties(
  library
  PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
              ${CMAKE_CURRENT_BINARY_DIR}/library-build/include)

install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/library-build/lib
        DESTINATION ${CMAKE_INSTALL_PREFIX})
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/library-build/include
        DESTINATION ${CMAKE_INSTALL_PREFIX})

add_library(${PROJECT_NAME} SHARED src/file.cpp)
target_link_libraries(${PROJECT_NAME} library)

```

The problem I’m encountering is build time, especially when built on arm64. It’s a bit faster when building for a second time but it can still take a few minutes - a bit annoying during development.  
What I was thinking of doing is exporting the library installed with `ExternalProject_Add` so it can be later found with `find_package()`. Then installation step could be skipped:

```cmake
find_library(LIBRARY_INSTALLED library)
if(NOT LIBRARY_INSTALLED)

  include(ExternalProject)

	file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/library-build/include)

	ExternalProject_Add(
  	library-build
  	.
  	.
  	.
  	)

```

I’ve tried using `install()` to export the library, but it can’t be done with `IMPORTED` libraries. My question is whether it is possible to export this type of library (that needs to be linked to the `.so` file) or create a target that can be exported so the library can be found using `find_package`. Maybe there is another solution or workaround to achieve my goal?

---

<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: [February 17, 2024, 5:50am UTC](https://discourse.cmake.org/t/export-third-part-library-built-with-externalproject-add/10038/2 "2024-02-17T05:50:27Z")

</div>

It’s probably better for that `ExternalProject` code to just always be found using `find_package` and to have it provide its own `-config.cmake` package file. You can then [`find_dependency`](https://cmake.org/cmake/help/latest/module/CMakeFindDependencyMacro.html) that package to get its targets.

Note that `add_library` and `ExternalProject_add` in the same project are like oil-and-water as CMake just doesn’t have insight into the EP build (as you can see from having to just “guess” `LOCATION` bits). It might work better to have a standalone “superbuild” that builds your dependency _and_ your project using `ExternalProject_add`.

---

<div class="post-metadata">

### Author: ![KmakD](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kmakd/32/4289_2.png) [@KmakD](https://discourse.cmake.org/u/KmakD)
#### Post date: [February 19, 2024, 11:34am UTC](https://discourse.cmake.org/t/export-third-part-library-built-with-externalproject-add/10038/3 "2024-02-19T11:34:44Z")

</div>

The problem is that this third-part library doesn’t provide `-config.cmake` file. That’s why I’m creating target and link it to `.so` file. I was trying to install that target but it can’t be done with `IMPORTED` libraries.

Later I’d like to have superbuild but for now I’m trying to figure out how to export and find that library.

---

<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: [February 19, 2024, 12:30pm UTC](https://discourse.cmake.org/t/export-third-part-library-built-with-externalproject-add/10038/4 "2024-02-19T12:30:34Z")

</div>

> [@KmakD](#):
>
> The problem is that this third-part library doesn’t provide `-config.cmake` file.

I would encourage you to work with upstream to do so. If they’re not willing, a `FindDep.cmake` module is probably best.

---

<div class="post-metadata">

### Author: ![KmakD](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kmakd/32/4289_2.png) [@KmakD](https://discourse.cmake.org/u/KmakD)
#### Post date: [February 21, 2024, 2:35pm UTC](https://discourse.cmake.org/t/export-third-part-library-built-with-externalproject-add/10038/5 "2024-02-21T14:35:31Z")

</div>

Your comment set me on the right path. I figured out that the library that I’m installing is actually build with `pkg-config` and I can use `PkgConfig` and `pkg_check_modules` to find it.  
Thanks for help 😁
