Commit 4db31095 authored by Daniele E. Domenichelli's avatar Daniele E. Domenichelli Committed by Brad King
Browse files

Fix link line order when shared libraries are de-duplicated

Since commit v3.1.0-rc1~227^2~1 (De-duplicate shared library targets in
generated link lines, 2014-07-30) we de-duplicate shared library targets
on the link line.  However, some toolchains will fail linking if an
executable is linking to a shared library that is not used directly and
a static library that depends on the shared one.  The linker may not
keep the reference to the shared library the first time and then the
symbols needed by the static library may not be found.

Fix this by reversing the direction of the for loop that removes the
duplicate shared libraries, in order to ensure that the last occurrence
of the library is left instead of the first one.

Extend Tests/Dependency with a case covering this behavior.  Create an
executable that links to a shared library and a static library but only
needs the shared library as a dependency of the static library.

Co-Author: Brad King <>
parent 06c3b7a8
......@@ -263,21 +263,26 @@ cmComputeLinkDepends::Compute()
// Compute the final set of link entries.
// Iterate in reverse order so we can keep only the last occurrence
// of a shared library.
std::set<int> emmitted;
for(std::vector<int>::const_iterator li = this->FinalLinkOrder.begin();
li != this->FinalLinkOrder.end(); ++li)
li = this->FinalLinkOrder.rbegin(),
le = this->FinalLinkOrder.rend();
li != le; ++li)
int i = *li;
LinkEntry const& e = this->EntryList[i];
cmTarget const* t = e.Target;
// Entries that we know the linker will re-use for symbols
// needed by later entries do not need to be repeated.
// Entries that we know the linker will re-use do not need to be repeated.
bool uniquify = t && t->GetType() == cmTarget::SHARED_LIBRARY;
if(!uniquify || emmitted.insert(i).second)
// Reverse the resulting order since we iterated in reverse.
std::reverse(this->FinalLinkEntries.begin(), this->FinalLinkEntries.end());
// Display the final set.
......@@ -51,3 +51,4 @@ add_subdirectory(Case1)
project(CASE5 C)
add_library(case5Foo SHARED foo.c)
add_library(case5Bar STATIC bar.c)
target_link_libraries(case5Bar case5Foo)
add_executable(case5 main.c)
target_link_libraries(case5 case5Foo case5Bar)
#ifdef _WIN32
void foo(void);
#include <stdio.h>
void bar(void)
#include <stdio.h>
#ifdef _WIN32
void foo(void)
void bar(void);
int main(int argc, char *argv[])
return 0;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment