target_link_libraries and include file

I install a libdatachannel library by myself,

on main.cpp

#include <rtc/rtc.hpp>

on CMakeLists.txt

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

The cmake show this error

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

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


target_link_libraries(app PRIVATE LibDataChannel::LibDataChannel)

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.

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 ?

`

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.