MantisBT - CMake
View Issue Details
0001644CMakeCMakepublic2005-03-05 10:132006-10-05 16:59
David Somers 
Brad King 
normalminoralways
closedfixed 
 
 
0001644: static library linking
When using CMake to link against static libraries, the process is complicated because it has a preference for dynamic/shared libraries.

Consider: TARGET_LINK_LIBRARIES(myexe /home/me/lib/libfoo.a)
or
LINK_LIBARIES(/home/me/lib/libfoo.a)

CMake should then be smart enough to pick up that libfoo is a static library, and so set the linker flags appropriatly.

The present situation is: You have to specifically add flags to tell it to build a static executable. You can either use the variable CMAKE_EXE_LINKER_FLAGS to set the flags for all executables, or set the LINK_FLAGS target property on a specific executable. Unfortunately right now there is no variable
that is automatically set with the appropriate flags for the platform, so you need a bunch of IF conditions (and maybe your own try-compiles)
to set it. We should probably add a target property for executables to specify whether it is linked statically or shared.
No tags attached.
Issue History

Notes
(0002285)
Bill Hoffman   
2005-04-05 13:45   
This is really a unix linker problem. If you specify a full path to a .a to the linker, many linkers will link in ALL the objects which is not what we want, but if you break it into -L and -l then you get this problem.
(0004922)
Brad King   
2006-09-15 14:11   
The following changes address this problem. Many linkers provide options to tell them to prefer archives over shared libraries and vice versa. Currently the flags are only enabled on Linux but others will follow.

/cvsroot/CMake/CMake/Modules/CMakeCXXInformation.cmake,v <-- CMakeCXXInformation.cmake
new revision: 1.13; previous revision: 1.12
/cvsroot/CMake/CMake/Modules/CMakeSystemSpecificInformation.cmake,v <-- CMakeSystemSpecificInformation.cmake
new revision: 1.36; previous revision: 1.35
/cvsroot/CMake/CMake/Modules/Platform/Linux.cmake,v <-- Linux.cmake
new revision: 1.11; previous revision: 1.10
/cvsroot/CMake/CMake/Source/cmLocalGenerator.cxx,v <-- cmLocalGenerator.cxx
new revision: 1.146; previous revision: 1.145
/cvsroot/CMake/CMake/Source/cmOrderLinkDirectories.cxx,v <-- cmOrderLinkDirectories.cxx
new revision: 1.31; previous revision: 1.30
/cvsroot/CMake/CMake/Source/cmOrderLinkDirectories.h,v <-- cmOrderLinkDirectories.h
new revision: 1.19; previous revision: 1.18
confused about file cmLocalGenerator.h -- ignoring
/cvsroot/CMake/CMake/Tests/SystemInformation/SystemInformation.in,v <-- SystemInformation.in
new revision: 1.6; previous revision: 1.5

(0004924)
Brad King   
2006-09-15 14:13   
Bug 0001643 is related to this bug.
(0004926)
Brad King   
2006-09-15 14:31   
The following changes add a test for this feature:

/cvsroot/CMake/CMake/Tests/Complex/Executable/CMakeLists.txt,v <-- CMakeLists.txt
new revision: 1.53; previous revision: 1.52
/cvsroot/CMake/CMake/Tests/Complex/Executable/complex.cxx,v <-- complex.cxx
new revision: 1.86; previous revision: 1.85
/cvsroot/CMake/CMake/Tests/Complex/Library/CMakeLists.txt,v <-- CMakeLists.txt
new revision: 1.31; previous revision: 1.30
/cvsroot/CMake/CMake/Tests/Complex/Library/TestLink.c,v <-- TestLink.c
initial revision: 1.1
(0004927)
Brad King   
2006-09-15 14:42   
Here is a summary of the flags needed on each platform to implement this feature.

Linux and FreeBSD:
  starts as shared-then-archive

  gcc -Wl,-Bstatic # archive only
  gcc -Wl,-Bdynamic # shared-then-archive

  g++ -Wl,-Bstatic # archive only
  g++ -Wl,-Bdynamic # shared-then-archive

  If -static is given go gcc then it is not safe to ever switch to
  -Bdynamic because it gives other different options to ld. If it is
  not given then shared-then-archive linking is the default.

HP-UX:
  starts as default

  cc -Wl,-a,default # shared-then-archive
  cc -Wl,-a,archive # archive only
  cc -Wl,-a,shared # shared only
  cc -Wl,-a,shared_archive # shared-then-archive
  cc -Wl,-a,archive_shared # archive-then-shared

  aCC -Wl,-a,default # shared-then-archive
  aCC -Wl,-a,archive # archive only
  aCC -Wl,-a,shared # shared only
  aCC -Wl,-a,shared_archive # shared-then-archive
  aCC -Wl,-a,archive_shared # archive-then-shared

  We probably want:

  cc -Wl,-a,archive # archive only
  cc -Wl,-a,default # shared-then-archive

  aCC -Wl,-a,archive # archive only
  aCC -Wl,-a,default # shared-then-archive

SUN:
  starts as shared-then-archive

  cc -Bstatic # archive only
  cc -Bdynamic # shared-then-archive

  CC -Bstatic # archive only
  CC -Bdynamic # shared-then-archive

AIX:
  starts as shared-then-archive

  xlc -bstatic # archive only
  xlc -bdynamic # shared-then-archive

  xlC -bstatic # archive only
  xlC -bdynamic # shared-then-archive

IRIX:
  starts as shared-then-archive

  cannot be changed!! (?)

OSX:
  starts as shared-then-archive

  cannot be changed!!

  There seems to be a separate link editor for static and dynamic
  links so either -dynamic or -static may be specified exactly once.
(0004928)
Brad King   
2006-09-15 14:58   
Enabled on HP and FreeBSD:

/cvsroot/CMake/CMake/Modules/Platform/HP-UX.cmake,v <-- HP-UX.cmake
new revision: 1.22; previous revision: 1.21
/cvsroot/CMake/CMake/Modules/Platform/FreeBSD.cmake,v <-- FreeBSD.cmake
new revision: 1.9; previous revision: 1.8
(0004929)
Brad King   
2006-09-15 15:05   
Enabled for Sun:

/cvsroot/CMake/CMake/Modules/Platform/SunOS.cmake,v <-- SunOS.cmake
new revision: 1.19; previous revision: 1.18
(0004930)
Brad King   
2006-09-15 15:15   
Fixed HP options that are passed directly to ld:

/cvsroot/CMake/CMake/Modules/Platform/HP-UX.cmake,v <-- HP-UX.cmake
new revision: 1.23; previous revision: 1.22
(0004931)
Brad King   
2006-09-15 15:19   
Enabled on AIX:

/cvsroot/CMake/CMake/Modules/Platform/AIX.cmake,v <-- AIX.cmake
new revision: 1.15; previous revision: 1.14
(0004940)
Brad King   
2006-09-16 11:52   
The following fixes were needed to cleanup the dashboard after implementing this feature:

/cvsroot/CMake/CMake/Source/cmLocalGenerator.cxx,v <-- cmLocalGenerator.cxx
new revision: 1.147; previous revision: 1.146
/cvsroot/CMake/CMake/Modules/Platform/SunOS.cmake,v <-- SunOS.cmake
new revision: 1.20; previous revision: 1.19
(0004957)
Brad King   
2006-09-18 09:40   
I'm guessing that QNX works like Linux:

/cvsroot/CMake/CMake/Modules/Platform/QNX.cmake,v <-- QNX.cmake
new revision: 1.3; previous revision: 1.2
(0005077)
Brad King   
2006-10-02 10:28   
I'm now closing this bug. The implementation seems to work on all intended platforms.
(0005268)
Brad King   
2006-10-05 15:09   
The following changes enable this feature on Cygwin, MinGW, and MSYS.

/cvsroot/CMake/CMake/Source/cmLocalGenerator.cxx,v <-- cmLocalGenerator.cxx
new revision: 1.158; previous revision: 1.157
/cvsroot/CMake/CMake/Source/cmOrderLinkDirectories.cxx,v <-- cmOrderLinkDirectories.cxx
new revision: 1.32; previous revision: 1.31
/cvsroot/CMake/CMake/Source/cmOrderLinkDirectories.h,v <-- cmOrderLinkDirectories.h
new revision: 1.20; previous revision: 1.19
/cvsroot/CMake/CMake/Tests/Complex/Executable/complex.cxx,v <-- complex.cxx
new revision: 1.88; previous revision: 1.87
/cvsroot/CMake/CMake/Modules/Platform/CYGWIN.cmake,v <-- CYGWIN.cmake
new revision: 1.16; previous revision: 1.15
/cvsroot/CMake/CMake/Modules/Platform/Windows-gcc.cmake,v <-- Windows-gcc.cmake
new revision: 1.17; previous revision: 1.16
(0005271)
Brad King   
2006-10-05 16:59   
/cvsroot/CMake/CMake/Source/cmLocalGenerator.cxx,v <-- cmLocalGenerator.cxx
new revision: 1.159; previous revision: 1.158
/cvsroot/CMake/CMake/Source/cmOrderLinkDirectories.cxx,v <-- cmOrderLinkDirectories.cxx
new revision: 1.33; previous revision: 1.32