add_dependencies could not able to find system default libraries in 3.28.3

In the cmake 3.28.3, when I give library ( example here is , in the add_dependencies, its throwing an error: The dependency target “dl” of target “xxxx” does not exist.
add_dependencies(xxxx zzzz dl)
The dl library is there in the system: libc6:amd64: /usr/lib/x86_64-linux-gnu/

How to solve this issue.

You need target_link_libraries() instead of add_dependencies(). The latter only takes cmake targets as arguments.

Thanks for your reply. There is already one target_link_libraries(xxxx glog gflags yyyy …) there, and when modified as per your advice,
target_link_libraries(xxxx glog gflags yyyy zzzz, dl)
its throwing the following error:
Target “zzzz” of type UTILITY may not be linked into another target. One
may link only to INTERFACE, OBJECT, STATIC or SHARED libraries, or to
executables with the ENABLE_EXPORTS property set.
I tried with separate target_link_libraries(xxxx zzzz, dl)
as well. Its throwing the same error
Any idea how to fix on this? This error is not coming when I use add_dependencies, but previous error is coming as I mentioned in my last mail.

Looking forward for your reply.

If I understand it right:

  • is a library on disk, external to your project.
  • zzzz is a custom target in your project (created by add_custom_target()).
  • You need your target xxxx to consume both.

add_dependencies() only accepts CMake targets as arguments, and introduces dependencies, but does not cause linking. target_link_libraries() accepts both targets and libraries, and causes them to be linked.

To link the library, use target_link_libraries(). To depend on a custom target (since these cannot be linked, they don’t correspond to anything linkable), use add_dependencies(). Put together:

add_dependencies(xxxx dl)
target_link_libraries(xxxx zzzz)

add_dependencies(xxxx dl) does not work on cmake 3.28.3 as I am seeing the same error. Probably this could not find the file.
The dependency target “dl” of target “xxxx” does not exist.
I added the following changes and its not throwing any error. Are these changes are fine or do we have any alternative way to fix this issue.
add_custom_target(custom_dl ${DL})
add_dependencies(xxxx custom_dl)

here add_custom_target is compulsory or any other way we can do it?


I think you confused zzzz and dl in your code example. It should be:

add_dependencies(xxxx zzzz)
target_link_libraries(xxxx dl)


if zzzz is a custom target and is the library file or a link to it (note: without .2 at the end)

This does nothing useful.

Thank you for an update. This is what I understood from your explanation. All installed libraries in the OS (like dl, gflags,… ) has to included using the target_link_libraries. And all the cmake generated targets need to be added using add_dependencies.

There is one more Question. If my target depends on the header file (x.h), how can I add that.
add_executible(xxxx < set of source files >)
target_link_libraries(xxxx yyyy zzzz)
add_dependencies(xxxx x.h)

This code snippet throwing the same error: The dependency target “x.h” of target “xxxx” does not
Note: this was working fine with 3.16.3 and seeing issues with 3.28.

Manually adding dependencies is rather the exception, e.g. when needing a specific build order.

It seems, you are unfamiliar with CMake, did you look at the tutorials?

Header files are source files. There are exceptions for generated headers in other directories.