CMake 2.6 Notes: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
No edit summary
Line 21: Line 21:


This worked most of the time, but some platforms (such as OS X) do not  
This worked most of the time, but some platforms (such as OS X) do not  
support the -Bstatic or equivalent flag.  This made it impossible to  
support the <code>-Bstatic</code> or equivalent flag.  This made it impossible to  
link to the static version of a library without creating a symlink in  
link to the static version of a library without creating a symlink in  
another directory and using that one instead.
another directory and using that one instead.
Line 37: Line 37:
   target_link_libraries(myexe /path/to/libA.so B)
   target_link_libraries(myexe /path/to/libA.so B)


where "B" is meant to link "/path/to/libB.so".  This code is incorrect  
where "<code>B</code>" is meant to link "<code>/path/to/libB.so</code>".  This code is incorrect  
because it asks CMake to link to B but does not provide the proper  
because it asks CMake to link to <code>B</code> but does not provide the proper  
linker search path for it.  It used to work by accident because the  
linker search path for it.  It used to work by accident because the  
-L/path/to would get added as part of the implementation of linking to  
<code>-L/path/to</code> would get added as part of the implementation of linking to  
A.  The correct code would be
A.  The correct code would be


Line 53: Line 53:


In order to support projects that have this bug, we've added a  
In order to support projects that have this bug, we've added a  
compatibility feature that adds the "-L/path/to" paths for all libraries  
compatibility feature that adds the "<code>-L/path/to</code>" paths for all libraries  
linked with full paths even though the linker will not need those paths  
linked with full paths even though the linker will not need those paths  
to find the main libraries.  The compatibility mode is enabled when a  
to find the main libraries.  The compatibility mode is enabled when a  
link line contains a non-full-path library (like B or -lB) and either  
link line contains a non-full-path library (like <code>B</code>) and either  
CMAKE_BACKWARDS_COMPATIBILITY is set to 2.4 or lower or  
<code>CMAKE_BACKWARDS_COMPATIBILITY</code> is set to 2.4 or lower or  
CMAKE_LINK_OLD_PATHS is set to true.
<code>CMAKE_LINK_OLD_PATHS</code> is set to true.

Revision as of 14:13, 28 January 2008

Exporting and Importing Targets

Linking

CMake 2.6 implements a new approach to generating link lines for targets.

Consider these libraries:

  /path/to/libfoo.a
  /path/to/libfoo.so

Previously if someone wrote

  target_link_libraries(myexe /path/to/libfoo.a)

CMake would generate this code to link it:

  ... -L/path/to -Wl,-Bstatic -lfoo -Wl,-Bdynamic ...

This worked most of the time, but some platforms (such as OS X) do not support the -Bstatic or equivalent flag. This made it impossible to link to the static version of a library without creating a symlink in another directory and using that one instead.

Now CMake will generate this code:

  ... /path/to/libfoo.a ...

This guarantees that the correct library is chosen.

There is a side-effect of this fix. Projects used to be able to write this (wrong) code and it would work by accident:

  add_executable(myexe myexe.c)
  target_link_libraries(myexe /path/to/libA.so B)

where "B" is meant to link "/path/to/libB.so". This code is incorrect because it asks CMake to link to B but does not provide the proper linker search path for it. It used to work by accident because the -L/path/to would get added as part of the implementation of linking to A. The correct code would be

  link_directories(/path/to)
  add_executable(myexe myexe.c)
  target_link_libraries(myexe /path/to/libA.so B)

or even better

  add_executable(myexe myexe.c)
  target_link_libraries(myexe /path/to/libA.so /path/to/libB.so)

In order to support projects that have this bug, we've added a compatibility feature that adds the "-L/path/to" paths for all libraries linked with full paths even though the linker will not need those paths to find the main libraries. The compatibility mode is enabled when a link line contains a non-full-path library (like B) and either CMAKE_BACKWARDS_COMPATIBILITY is set to 2.4 or lower or CMAKE_LINK_OLD_PATHS is set to true.