# target\_link\_libraries and include file

**URL:** https://discourse.cmake.org/t/target-link-libraries-and-include-file/9567
**Category:** Usage
**Tags:** os:windows
**Created:** [December 6, 2023, 5:08am UTC](https://discourse.cmake.org/t/target-link-libraries-and-include-file/9567 "2023-12-06T05:08:59Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Sea\_Sea](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sea_sea/32/4025_2.png) [@Sea\_Sea](https://discourse.cmake.org/u/Sea_Sea)
#### Post date: [December 6, 2023, 5:08am UTC](https://discourse.cmake.org/t/target-link-libraries-and-include-file/9567/1 "2023-12-06T05:08:59Z")

</div>

I install a libdatachannel library by myself,

on main.cpp

```auto
#include <rtc/rtc.hpp>

```

on CMakeLists.txt

```auto
...
target_link_libraries(app PRIVATE ${LibDataChannel_LIBRARIES})

```

The cmake show this error

```auto
fatal error C1083: can not open: 'rtc/rtc.hpp': No such file or directory

```

But I change the CMakeLists.txt content, it will success.

```auto

target_link_libraries(app PRIVATE LibDataChannel::LibDataChannel)

```

How to let it work for ${LibDataChannel\_LIBRARIES} ?

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [December 6, 2023, 1:20pm UTC](https://discourse.cmake.org/t/target-link-libraries-and-include-file/9567/2 "2023-12-06T13:20:11Z")

</div>

> [@Sea\_Sea](#):
>
> How to let it work for ${LibDataChannel\_LIBRARIES} ?

Why do you need it to work for this? The modern CMake approach is to use targets instead of variables, so linking `LibDataChannel::LibDataChannel` is the preferred thing to do.

---

<div class="post-metadata">

### Author: ![Sea\_Sea](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sea_sea/32/4025_2.png) [@Sea\_Sea](https://discourse.cmake.org/u/Sea_Sea)
#### Post date: [December 7, 2023, 12:57am UTC](https://discourse.cmake.org/t/target-link-libraries-and-include-file/9567/3 "2023-12-07T00:57:32Z")

</div>

OK, the modern CMake approach is use targets `lib:lib`.

So you mean the variables approach `target\_link\_libraries(app PRIVATE ${LibDataChannel\_LIBRARIES}) is deprecated?

or I miss some step for the variables approach ?

`

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [December 8, 2023, 10:26am UTC](https://discourse.cmake.org/t/target-link-libraries-and-include-file/9567/4 "2023-12-08T10:26:20Z")

</div>

I wouldn’t say linking libraries directly is outright deprecated, but it’s certainly discouraged if the package you’re linking to provides CMake targets for linking.

To be fair, in the end it all boils down to what the package provides in terms of CMake support. Ideally, it will provide CMake target(s) with appropriate usage requirements (as `LibDataChannel` apparently does). The content of `PackageName_LIBRARIES` is also under the package’s control — it can contain just the bare path to the libraries (in which case you need to provide include directories and other usage requirements manually), or it could contain the target names.

When consuming an external package, you always have to check what it provides. If there are targets, use them, it’s the modern approach.
