Invalid handling of linker flags during build

Bug #1203786 reported by dobey
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
cmake (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

When building a project using cmake, which contains a shared library, and any code which uses that shared library, such as test programs, installed application, or plug-ins for other applications or libraries, cmake incorrectly applies all of the linker flags from the shared library, to all of the resulting objects which depend on it. This makes using certain linker features, such as version scripts, very problematic.

This problem is then compounded when building with a builddir != srcdir setup, as the possible workaround of using a relative file name for --version-script and including an additional version script which exports all the symbols, cannot be used.

The linker flags for a shared library should never be copied over to the other targets which depend upon that library.

Revision history for this message
Brad King (brad.king) wrote :

It sounds like you're trying to use target_link_libraries to pass link flags that are not libraries. There is a separate target property for this:

 http://www.cmake.org/cmake/help/v2.8.11/cmake.html#prop_tgt:LINK_FLAGS

Revision history for this message
dobey (dobey) wrote :

@Brad, the documentation for target_link_libraries() clearly states that passing any argument that begins with - is treated as a linker flag, and is an accepted way to pass linker flags. It however, does not mention anywhere that the flags, or libraries that the target is linking to, will be copied to every other target which has a dependency on the target. There is no good reason to copy the flags of one target to any other target. If that was the behavior I had wanted, I would have just set CMAKE_LINKER_FLAGS directly, so it would be applied to everything.

Revision history for this message
dobey (dobey) wrote :

I've also tried to use set_property() to set LINK_FLAGS property just now, but its behavior breaks as well in some cases, and I end up with something like "-loauth;-lsecret-1;-lglib-2.0;-lgio-2.0" or similar, when passing in a variable which was set by use of the pkg-config commands.

Revision history for this message
Brad King (brad.king) wrote :

From target_link_libraries documentation:

 http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:target_link_libraries
 "Library dependencies are transitive by default. When this target is linked into another target then the libraries linked to this target will appear on the link line for the other target too."

For shared libraries one can set the LINK_INTERFACE_LIBRARIES property to disable transitive behavior (see also the LINK_PRIVATE/LINK_PUBLIC signature of target_link_libraries). For static libraries there is no way to disable transitive linking because it is necessary to satisfy link dependencies of the objects in the static library itself.

Link flags meant to adjust library search behavior can go in target_link_libraries calls, but those meant for things like linker scripts should go in LINK_FLAGS.

Revision history for this message
Gianfranco Costamagna (costamagnagianfranco) wrote :

Hi, I'm setting to invalid for now, seems that obviously target_link_libraries should be transitive for flags.

Changed in cmake (Ubuntu):
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.