CMP0003.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. CMP0003
  2. -------
  3. .. |REMOVED_IN_CMAKE_VERSION| replace:: 4.0
  4. .. include:: include/REMOVED_PROLOGUE.rst
  5. Libraries linked via full path no longer produce linker search paths.
  6. This policy affects how libraries whose full paths are NOT known are
  7. found at link time, but was created due to a change in how CMake deals
  8. with libraries whose full paths are known. Consider the code
  9. .. code-block:: cmake
  10. target_link_libraries(myexe /path/to/libA.so)
  11. CMake 2.4 and below implemented linking to libraries whose full paths
  12. are known by splitting them on the link line into separate components
  13. consisting of the linker search path and the library name. The
  14. example code might have produced something like
  15. ::
  16. ... -L/path/to -lA ...
  17. in order to link to library A. An analysis was performed to order
  18. multiple link directories such that the linker would find library A in
  19. the desired location, but there are cases in which this does not work.
  20. CMake versions 2.6 and above use the more reliable approach of passing
  21. the full path to libraries directly to the linker in most cases. The
  22. example code now produces something like
  23. ::
  24. ... /path/to/libA.so ....
  25. Unfortunately this change can break code like
  26. .. code-block:: cmake
  27. target_link_libraries(myexe /path/to/libA.so B)
  28. where ``B`` is meant to find ``/path/to/libB.so``. This code is wrong
  29. because the user is asking the linker to find library B but has not
  30. provided a linker search path (which may be added with the
  31. link_directories command). However, with the old linking
  32. implementation the code would work accidentally because the linker
  33. search path added for library A allowed library B to be found.
  34. In order to support projects depending on linker search paths added by
  35. linking to libraries with known full paths, the ``OLD`` behavior for this
  36. policy will add the linker search paths even though they are not
  37. needed for their own libraries. When this policy is set to ``OLD``, CMake
  38. will produce a link line such as
  39. ::
  40. ... -L/path/to /path/to/libA.so -lB ...
  41. which will allow library B to be found as it was previously. When
  42. this policy is set to NEW, CMake will produce a link line such as
  43. ::
  44. ... /path/to/libA.so -lB ...
  45. which more accurately matches what the project specified.
  46. The setting for this policy used when generating the link line is that
  47. in effect when the target is created by an add_executable or
  48. add_library command. For the example described above, the code
  49. .. code-block:: cmake
  50. cmake_policy(SET CMP0003 OLD) # or cmake_policy(VERSION 2.4)
  51. add_executable(myexe myexe.c)
  52. target_link_libraries(myexe /path/to/libA.so B)
  53. will work and suppress the warning for this policy. It may also be
  54. updated to work with the corrected linking approach:
  55. .. code-block:: cmake
  56. cmake_policy(SET CMP0003 NEW) # or cmake_policy(VERSION 2.6)
  57. link_directories(/path/to) # needed to find library B
  58. add_executable(myexe myexe.c)
  59. target_link_libraries(myexe /path/to/libA.so B)
  60. Even better, library B may be specified with a full path:
  61. .. code-block:: cmake
  62. add_executable(myexe myexe.c)
  63. target_link_libraries(myexe /path/to/libA.so /path/to/libB.so)
  64. When all items on the link line have known paths CMake does not check
  65. this policy so it has no effect.
  66. Note that the warning for this policy will be issued for at most one
  67. target. This avoids flooding users with messages for every target
  68. when setting the policy once will probably fix all targets.
  69. .. |INTRODUCED_IN_CMAKE_VERSION| replace:: 2.6.0
  70. .. |WARNED_OR_DID_NOT_WARN| replace:: warned
  71. .. include:: include/REMOVED_EPILOGUE.rst