I want to write some executable code which depends on some third-party libraries and a self-define library(mylib), Is there any way to enforce the target_link_libraries performs after mylib has been created?
Asked
Active
Viewed 18 times
0
-
Why not just do `target_link_libraries` before `mylib` is created? – KamilCuk Jun 13 '22 at 08:22
-
I used a variable ALL_LIBRARIES to store all libraries that I need, and use list(APPEND ALL_LIBRARIES mylib) to update it. Finally linked the target with ${ALL_LIBRARIES} use target_link_libraries(target ${ALL_LIBRARIES}) – Zhang Zheng Jun 13 '22 at 08:28
-
1I do not understand. Sure, so why not just do `target_link_libraries(... mylib)` from the start? – KamilCuk Jun 13 '22 at 08:29
-
Inspired by you, I did some change: directly link the target with mylib by target_lint_libraries(target ${ALL_LIBRARIES} mylib), and it success, thanks – Zhang Zheng Jun 13 '22 at 08:30
1 Answers
1
The target doesn't have to exist to link with it. Just target_link_libraries(... mylib)
from the start, and define target mylib
later.
When you forget about defining mylib
target, you will get -lmylib: could not find library
message from the linker, or messages about missing headers when compiling (depending if the target exports some include paths).
Is there any way to enforce the target_link_libraries performs after mylib has been created?
Yes, you can overwrite add_library
function with a custom handler that checks the name of the library and executes specific action on it. For example 'Overwrite' cmake command using a macro and restore its default behavior .

KamilCuk
- 120,984
- 8
- 59
- 111