# target\_link\_libraries() requires full path for some but not all project libraries?

**URL:** https://discourse.cmake.org/t/target-link-libraries-requires-full-path-for-some-but-not-all-project-libraries/2097
**Category:** Usage
**Created:** [October 30, 2020, 6:47pm UTC](https://discourse.cmake.org/t/target-link-libraries-requires-full-path-for-some-but-not-all-project-libraries/2097 "2020-10-30T18:47:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tomoreilly](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomoreilly](https://discourse.cmake.org/u/tomoreilly)
#### Post date: [October 30, 2020, 6:47pm UTC](https://discourse.cmake.org/t/target-link-libraries-requires-full-path-for-some-but-not-all-project-libraries/2097/1 "2020-10-30T18:47:03Z")

</div>

My project builds several libraries, then links them to generate an executable. When calling target\_link\_libraries() for the executable, some - but not all - of the project libraries must be specified with full path ${CMAKE\_BINARY\_DIR/src/dir, else the linker cannot find them. Yet other project libraries don’t require this. Why? Each project library was added to the project with add\_library().

My executable’s CMakeLists.txt looks like this:

```
add_executable(mbgrdviz
               mbgrdviz_callbacks.c mbgrdviz_creation.c mbgrdviz_main.c)

target_include_directories(mbgrdviz
                           PUBLIC
                           .
                           ${GMT_INCLUDE_DIR}
                           ${CMAKE_SOURCE_DIR}/src/mbio
                           ${CMAKE_SOURCE_DIR}/src/mbaux
                           ${CMAKE_SOURCE_DIR}/src/mbview
                           ${CMAKE_SOURCE_DIR}/src/bsio
                           ${CMAKE_SOURCE_DIR}/src/surf
                           ${CMAKE_SOURCE_DIR}/src/gsf)
                           
# Why do libbsio.a, libmbsapi.a and libgsf.a require full path else
# they aren't found by linker?
target_link_libraries(mbgrdviz
                      PRIVATE libmbio.a
                      PRIVATE libmbaux.a
                      PRIVATE libmbview.a
                      PRIVATE ${CMAKE_BINARY_DIR}/src/bsio/libbsio.a
                      PRIVATE ${CMAKE_BINARY_DIR}/src/surf/libmbsapi.a
                      PRIVATE ${CMAKE_BINARY_DIR}/src/gsf/libgsf.a
                      PRIVATE gmt
                      PRIVATE Xm
                      PRIVATE Xt
                      PRIVATE X11
                      PRIVATE GL
                      PRIVATE GLU
                      PRIVATE m
                      PRIVATE netcdf
                      PRIVATE gdal
                      PRIVATE proj
                      PRIVATE pthread)

```

[target\_link\_libraries() documentation](https://cmake.org/cmake/help/v3.12/command/target_link_libraries.html) says that libraries may be specified by name only (as several of them are).  
Thanks!

---

<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: [October 30, 2020, 7:44pm UTC](https://discourse.cmake.org/t/target-link-libraries-requires-full-path-for-some-but-not-all-project-libraries/2097/2 "2020-10-30T19:44:14Z")

</div>

Your own libraries should be specified by **target name** , not file name.

---

<div class="post-metadata">

### Author: ![Dakon](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dakon/32/854_2.png) [@Dakon](https://discourse.cmake.org/u/Dakon)
#### Post date: [October 30, 2020, 7:46pm UTC](https://discourse.cmake.org/t/target-link-libraries-requires-full-path-for-some-but-not-all-project-libraries/2097/3 "2020-10-30T19:46:47Z")

</div>

Tom O’Reilly wrote:

> My project builds several libraries, then links them to generate an  
> executable. When calling target\_link\_libraries() for the executable, some -  
> but not all - of the project libraries must be specified with full path  
> ${CMAKE\_BINARY\_DIR/src/dir, else the linker cannot find them. Yet other  
> project libraries don’t require this. Why? Each project library was added  
> to the project with add\_library().

> ```
> # Library names with ‘lib’ prefix are built by this project,
> # others are third-party already installed on system.
> # Some of my project libraries - libbsio.a, libmbsapi.a,
> # libgsf.a - require full pathname else they are not found
> # by linker - why is this?
> target_link_libraries(mbgrdviz
> PRIVATE libmbio.a
> PRIVATE libmbaux.a
> PRIVATE libmbview.a
> PRIVATE ${CMAKE_BINARY_DIR}/src/bsio/libbsio.a
> PRIVATE ${CMAKE_BINARY_DIR}/src/surf/libmbsapi.a
> PRIVATE ${CMAKE_BINARY_DIR}/src/gsf/libgsf.a
> PRIVATE gmt
> PRIVATE Xm
> PRIVATE Xt
> PRIVATE X11
> PRIVATE GL
> PRIVATE GLU
> PRIVATE m
> PRIVATE netcdf
> PRIVATE gdal
> PRIVATE proj
> PRIVATE pthread)
> 
> ```

That means you should pass the _target_ name: if you do add\_library(mbio),  
then you do target\_link\_libraries(mbgrdviz PRIVATE mbio). Btw, you need to  
specify PRIVATE only once.

Eike

---

<div class="post-metadata">

### Author: ![tomoreilly](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomoreilly](https://discourse.cmake.org/u/tomoreilly)
#### Post date: [October 30, 2020, 11:47pm UTC](https://discourse.cmake.org/t/target-link-libraries-requires-full-path-for-some-but-not-all-project-libraries/2097/4 "2020-10-30T23:47:15Z")

</div>

That fixes it - thanks!
