# Link a Target to a custom built library

**URL:** https://discourse.cmake.org/t/link-a-target-to-a-custom-built-library/8403
**Category:** Usage
**Tags:** os:linux
**Created:** [June 23, 2023, 9:14pm UTC](https://discourse.cmake.org/t/link-a-target-to-a-custom-built-library/8403 "2023-06-23T21:14:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)
#### Post date: [June 23, 2023, 9:14pm UTC](https://discourse.cmake.org/t/link-a-target-to-a-custom-built-library/8403/1 "2023-06-23T21:14:21Z")

</div>

I have a target executable, app. Which I am trying to link to a libFoo.a, which is not built by CMake, but can be built by invoking a script, build.sh. Is there a way I can link `app` to a custom built libFoo.a?

I’ve tried the following…

```auto
add_custom_command(
   OUTPUT libFoo.a
   COMMAND build.sh)

add_libary(Foo STATIC IMPORTED GLOBAL) # Not sure if GLOBAL is necessary
set_target_properties(Foo PROPERTIES IMPORTED_LOCATION libFoo.a)

add_executable(app main.cpp)

target_link_libraries(app
   Foo # or libFoo.a
)

```

But I often get the error that there is `No rule to make target libFoo.a` I’ve played around with pathing around libFoo.a (output in BINARY\_DIR, explicitly specificying paths), but can’t seem to accomplish what I’m looking for. Any recommendations or a better approach to take?

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [June 24, 2023, 7:33pm UTC](https://discourse.cmake.org/t/link-a-target-to-a-custom-built-library/8403/2 "2023-06-24T19:33:28Z")

</div>

A custom command is not sufficient, you also need a target that depends on the output of this custom command, e.g. a custom target, and then a target level dependency between that target and the consuming target.

---

<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 25, 2023, 8:10pm UTC](https://discourse.cmake.org/t/link-a-target-to-a-custom-built-library/8403/3 "2023-06-25T20:10:39Z")

</div>

Adding this in there should help:

```cmake
add_library(FooReal INTERFACE)
add_dependencies(FooReal libFoo.a)
target_link_libraries(FooReal INTERFACE Foo)

```

I’d also recommend using absolute paths for `libFoo.a`.

---

<div class="post-metadata">

### Author: ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)
#### Post date: [June 26, 2023, 2:17pm UTC](https://discourse.cmake.org/t/link-a-target-to-a-custom-built-library/8403/4 "2023-06-26T14:17:05Z")

</div>

Can you give a bit more context? I tried out your suggestion to get the following…

```auto
add_custom_command(
   OUTPUT libFoo.a
   COMMAND build.sh)

add_libary(Foo STATIC IMPORTED GLOBAL) # Not sure if GLOBAL is necessary
set_target_properties(Foo PROPERTIES IMPORTED_LOCATION libFoo.a)

add_library(FooReal INTERFACE)
add_dependencies(FooReal libFoo.a)
target_link_libraries(FooReal INTERFACE Foo)

add_executable(app main.cpp)

target_link_libraries(app
   FooReal
)

```

but I still get the following error, ` No rule to make target 'libFoo.a', needed by 'app'`.

---

<div class="post-metadata">

### Author: ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)
#### Post date: [June 26, 2023, 2:44pm UTC](https://discourse.cmake.org/t/link-a-target-to-a-custom-built-library/8403/5 "2023-06-26T14:44:50Z")

</div>

I ended up finding this question on Stack Overflow, [custom target as a target library in cmake - Stack Overflow](https://stackoverflow.com/q/31274577/5374468).

With this I ended up using this pattern, which worked.

```auto
add_custom_command(
   OUTPUT libFoo.a
   COMMAND build.sh)

add_custom_target(buildLibFoo DEPENDS libFoo.a)

add_libary(Foo STATIC IMPORTED GLOBAL) # Not sure if GLOBAL is necessary
add_dependencies(Foo builLibFoo)
set_target_properties(Foo PROPERTIES IMPORTED_LOCATION libFoo.a)

add_executable(app main.cpp)

target_link_libraries(app
   Foo
)

```

@ben.boeckel, I’d still be curious what your solution would have looked like.

---

<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 27, 2023, 1:52am UTC](https://discourse.cmake.org/t/link-a-target-to-a-custom-built-library/8403/6 "2023-06-27T01:52:43Z")

</div>

The `IMPORTED_LOCATION` property should be an absolute path to `libFoo.a` here I believe.
