MantisBT - CMake
View Issue Details
0008843CMakeCMakepublic2009-04-06 07:372009-04-06 11:14
Tobias Rudolph 
Brad King 
normalmajoralways
closedfixed 
CMake-2-6 
 
0008843: Imported targets that are linked transitively to parent projects are not resolved correctly
I am trying to build a project containing several subprojects that are included via add_subdirectory. Now, here is an example demonstrating the problem:

The test project contains the following CMakeLists.txt:
----
project( CMakeTest )
cmake_minimum_required( VERSION 2.6 )

add_subdirectory( foo )
include_directories( foo )

add_executable( TestApp main.cpp )
target_link_libraries( TestApp foo )
----

The CMakeLists.txt of the subdirectory libFoo:
----
add_library( ImportedTarget SHARED IMPORTED )
set_target_properties( ImportedTarget PROPERTIES IMPORTED_LOCATION "/opt/local/lib/libz.dylib" )

add_library( foo SHARED foo.cpp )
target_link_libraries( foo ImportedTarget )
----

According to the documentation, the imported target ("ImportedTarget") is only known within "foo" and its subdirectories. So, when I try to link 'TestApp', it fails because the linker is looking for 'ImportedTarget' rather than '/opt/local/lib/libz.dylib' due to transitive linking (I suspect).

example attached
No tags attached.
gz CMake_transitive_linking.tar.gz (634) 2009-04-06 07:37
https://public.kitware.com/Bug/file/2153/CMake_transitive_linking.tar.gz
Issue History
2009-04-06 07:37Tobias RudolphNew Issue
2009-04-06 07:37Tobias RudolphFile Added: CMake_transitive_linking.tar.gz
2009-04-06 10:06Bill HoffmanStatusnew => assigned
2009-04-06 10:06Bill HoffmanAssigned To => Brad King
2009-04-06 10:10Alexandre FeblotNote Added: 0015920
2009-04-06 10:22Brad KingNote Added: 0015925
2009-04-06 11:12Brad KingNote Added: 0015930
2009-04-06 11:14Brad KingNote Added: 0015931
2009-04-06 11:14Brad KingStatusassigned => closed
2009-04-06 11:14Brad KingResolutionopen => fixed

Notes
(0015920)
Alexandre Feblot   
2009-04-06 10:10   
I second that. Why would imported target not behave as any other target regarding scopes? They should be global too.
(0015925)
Brad King   
2009-04-06 10:22   
Imported targets cannot be globally unique. Multiple subdirectories may do find_package(foo) and each import their own 'foo' with different options. Imported targets need to be scoped like variables and the link_directories command.

The reporter's analysis is correct. The proper fix is to make the transitive dependency analysis look in the right place for the imported target.
(0015930)
Brad King   
2009-04-06 11:12   
I've committed a fix and a test.

BUG: Lookup transitive link deps in depender scope
/cvsroot/CMake/CMake/Source/cmComputeLinkDepends.cxx,v <-- Source/cmComputeLinkDepends.cxx
new revision: 1.30; previous revision: 1.29
/cvsroot/CMake/CMake/Source/cmComputeLinkDepends.h,v <-- Source/cmComputeLinkDepends.h
new revision: 1.16; previous revision: 1.15

ENH: Test transitive link to subdir-imported lib
/cvsroot/CMake/CMake/Tests/ExportImport/Import/A/CMakeLists.txt,v <-- Tests/ExportImport/Import/A/CMakeLists.txt
initial revision: 1.1
/cvsroot/CMake/CMake/Tests/ExportImport/Import/A/imp_lib1.c,v <-- Tests/ExportImport/Import/A/imp_lib1.c
initial revision: 1.1
/cvsroot/CMake/CMake/Tests/ExportImport/Import/A/imp_mod1.c,v <-- Tests/ExportImport/Import/A/imp_mod1.c
initial revision: 1.1
/cvsroot/CMake/CMake/Tests/ExportImport/Import/A/imp_testExe1.c,v <-- Tests/ExportImport/Import/A/imp_testExe1.c
initial revision: 1.1
/cvsroot/CMake/CMake/Tests/ExportImport/Import/CMakeLists.txt,v <-- Tests/ExportImport/Import/CMakeLists.txt
new revision: 1.7; previous revision: 1.6
/cvsroot/CMake/CMake/Tests/ExportImport/Import/imp_mod1.c,v <-- Tests/ExportImport/Import/imp_mod1.c
new revision: delete; previous revision: 1.2
/cvsroot/CMake/CMake/Tests/ExportImport/Import/imp_testExe1.c,v <-- Tests/ExportImport/Import/imp_testExe1.c
new revision: delete; previous revision: 1.4
/cvsroot/CMake/CMake/Tests/ExportImport/Import/imp_testTransExe1.c,v <-- Tests/ExportImport/Import/imp_testTransExe1.c
initial revision: 1.1
(0015931)
Brad King   
2009-04-06 11:14   
Transitive link dependencies are now looked up in the scope of their immediate depender.

I've scheduled this for inclusion in 2.6.4.