# Problems when porting a library target from ExternalProject\_Add

**URL:** <https://discourse.cmake.org/t/problems-when-porting-a-library-target-from-externalproject-add/3522>\
**Category:** Code\
**Created:** [June 8, 2021, 4:31am UTC](https://discourse.cmake.org/t/problems-when-porting-a-library-target-from-externalproject-add/3522 "2021-06-08T04:31:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![psionic12](https://discourse.cmake.org/user_avatar/discourse.cmake.org/psionic12/32/998_2.png) [@psionic12](https://discourse.cmake.org/u/psionic12)\
**Post date:** [June 8, 2021, 4:31am UTC](https://discourse.cmake.org/t/problems-when-porting-a-library-target-from-externalproject-add/3522/1 "2021-06-08T04:31:50Z")

</div>

I’m trying to import a Rust project to my C++ project, I write the CMake code as below:

```
include(ExternalProject)
ExternalProject_Add(
        quiche_source
        GIT_REPOSITORY https://github.com/cloudflare/quiche.git
        CONFIGURE_COMMAND ""
        BUILD_COMMAND cargo build "--$<$<CONFIG:Release>: release>"
        BUILD_IN_SOURCE true
        INSTALL_COMMAND ""
)
ExternalProject_Get_Property(quiche_source SOURCE_DIR)

add_library(quiche STATIC IMPORTED GLOBAL)
set_property(TARGET quiche
        PROPERTY IMPORTED_LOCATION ${SOURCE_DIR}/target/$<LOWER_CASE:${CMAKE_BUILD_TYPE}>/libquiche.a
        PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${SOURCE_DIR}/include)
if (APPLE)
    target_link_libraries(quiche INTERFACE "-framework Security")
endif ()
add_dependencies(quiche quiche_source)

```

what I want is to download the “quiche” project, build it, and export it as a CMake target for later use. But when I load the CMakeLists.txt, CMake warns me that

> Imported target “quiche” includes non-existent path
> 
> ```
> "/Users/xxx/source/qunar_http3/cmake-build-debug/external/quiche/quiche_source-prefix/src/quiche_source/target/debug/libquiche.a"
> 
> ```

My guess is that `IMPORT` is for prebuilt library, but at this time CMake is just processing CMakeLists, so what is the right way to export the library, which is build by ExternalProject\_Add?

---

<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:** [June 8, 2021, 11:18am UTC](https://discourse.cmake.org/t/problems-when-porting-a-library-target-from-externalproject-add/3522/2 "2021-06-08T11:18:11Z")

</div>

Imported targets must exist at the time they are configured, so this won’t work. I think if you make it an `INTERFACE` library which brings in the library; that may work.

Also, don’t you need to specify to make a static library manually? Or is quiche targeting a `staticlib` by default (all of my crates make `.rlib` files instead, but they use the default settings)?

---

<div class="post-metadata">

**Author:** ![psionic12](https://discourse.cmake.org/user_avatar/discourse.cmake.org/psionic12/32/998_2.png) [@psionic12](https://discourse.cmake.org/u/psionic12)\
**Post date:** [June 10, 2021, 6:58am UTC](https://discourse.cmake.org/t/problems-when-porting-a-library-target-from-externalproject-add/3522/3 "2021-06-10T06:58:59Z")

</div>

INTERFACE library works for me, here’s my code, hope it could help others:

```
include(FetchContent)
message("downloading quiche...")
FetchContent_Declare(
        quiche_source
        GIT_REPOSITORY https://github.com/cloudflare/quiche.git
        GIT_TAG 0.8.1
        GIT_PROGRESS true
        GIT_SUBMODULES ""
        GIT_SUBMODULES_RECURSE off
)
FetchContent_Populate(quiche_source)

...

add_library(quiche INTERFACE libquiche.a)
target_include_directories(quiche INTERFACE ${quiche_source_SOURCE_DIR}/include)
target_link_libraries(quiche INTERFACE ${QUICHE_LIBRARY_PATH}/libquiche.a)

add_custom_command(
        OUTPUT libquiche.a
        COMMAND cargo ${QUICHE_BUILD_ARGS}
        WORKING_DIRECTORY ${quiche_source_SOURCE_DIR}
        COMMENT "start building quiche ${QUICHE_BUILD_ARGS}"
        VERBATIM
)
```

---

<div class="post-metadata">

**Author:** ![psionic12](https://discourse.cmake.org/user_avatar/discourse.cmake.org/psionic12/32/998_2.png) [@psionic12](https://discourse.cmake.org/u/psionic12)\
**Post date:** [June 10, 2021, 7:02am UTC](https://discourse.cmake.org/t/problems-when-porting-a-library-target-from-externalproject-add/3522/4 "2021-06-10T07:02:21Z")

</div>

The code below only works in 3.19, I didn’t find a way to accomplish this in lower version.

---

<div class="post-metadata">

**Author:** ![barcharcraz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/barcharcraz/32/1558_2.png) [@barcharcraz](https://discourse.cmake.org/u/barcharcraz)\
**Post date:** [June 14, 2021, 5:32am UTC](https://discourse.cmake.org/t/problems-when-porting-a-library-target-from-externalproject-add/3522/5 "2021-06-14T05:32:43Z")

</div>

what breaks in versions before 3.19, I don’t see anything relevant in the release notes
