Skip to content

Clarify treatment of link flags in target_link_libraries

John Beard requested to merge johnbeard/cmake:master into master

Say a link executable is set up as so:

set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> <LINK_LIBRARIES>")

When using target_link_libraries, the documentation states that items starting with '-' (except '-l' and '-framework') "are treated as linker flags". However, in the eventual link command, the items are placed among the <LINK_LIBRARIES>, rather than with <LINK_FLAGS>, so this:

target_link_libraries(target libraryName --extraFlag)

produces:

linkprog --toolchain --link --flags targetObject.obj -llibraryName --extraFlag

when it might have been expected to add --extraFlag like this:

linkprog --toolchain --link --flags  --extraFlag targetObject.obj -llibraryName

This makes sense if you read "are treated as linker flags" to mean "just not treated a linked library". I think adding something to the documentation along the lines of this patch might make it clearer?

Merge request reports