How to enforce cmake performs "traget_link_libraries" after a library has been created

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?

>Solution :

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 .

Leave a Reply