wrong build order with generator expression TARGET_LINKER_FILE

top-level CMakeLists.txt has


MyLib/CMakeLists.txt has

add_library(MyLib <sources>)

Tests/CMakeLists.txt has

add_executable(Test1 <sources>)
target_link_libraries(Test1 PRIVATE $<TARGET_LINKER_FILE:MyLib>)

Works under Linux. Under Windows, however, the command

cmake --build . --config Release

fails because the system tries to build Test1 before MyLib.

What am I doing wrong?

Don’t link to the resulting file. Link to the “MyLib” target. That way CMake will set the dependency right.

1 Like

When then would one use the generator expression?

When you need to access the file directly: for example, to copy it somewhere in a custom command, or to pass it to a script.

Generator expressions such as this one can be useful for more advanced/niche uses, but for basic everyday usage, you shouldn’t need them.

1 Like

OK, with plain MyLib in place of the generator expression everything works.

Thank you very much, Jakub and Petr.