CMP0060.rst 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. CMP0060
  2. -------
  3. .. |REMOVED_IN_CMAKE_VERSION| replace:: 4.0
  4. .. include:: include/REMOVED_PROLOGUE.rst
  5. .. versionadded:: 3.3
  6. Link libraries by full path even in implicit directories.
  7. Policy :policy:`CMP0003` was introduced with the intention of always
  8. linking library files by full path when a full path is given to the
  9. :command:`target_link_libraries` command. However, on some platforms
  10. (e.g. HP-UX) the compiler front-end adds alternative library search paths
  11. for the current architecture (e.g. ``/usr/lib/<arch>`` has alternatives
  12. to libraries in ``/usr/lib`` for the current architecture).
  13. On such platforms the :command:`find_library` may find a library such as
  14. ``/usr/lib/libfoo.so`` that does not belong to the current architecture.
  15. Prior to policy :policy:`CMP0003` projects would still build in such
  16. cases because the incorrect library path would be converted to ``-lfoo``
  17. on the link line and the linker would find the proper library in the
  18. arch-specific search path provided by the compiler front-end implicitly.
  19. At the time we chose to remain compatible with such projects by always
  20. converting library files found in implicit link directories to ``-lfoo``
  21. flags to ask the linker to search for them. This approach allowed existing
  22. projects to continue to build while still linking to libraries outside
  23. implicit link directories via full path (such as those in the build tree).
  24. CMake does allow projects to override this behavior by using an
  25. :ref:`IMPORTED library target <Imported Targets>` with its
  26. :prop_tgt:`IMPORTED_LOCATION` property set to the desired full path to
  27. a library file. In fact, many :ref:`Find Modules` are learning to provide
  28. :ref:`Imported Targets` instead of just the traditional ``Foo_LIBRARIES``
  29. variable listing library files. However, this makes the link line
  30. generated for a library found by a Find Module depend on whether it
  31. is linked through an imported target or not, which is inconsistent.
  32. Furthermore, this behavior has been a source of confusion because the
  33. generated link line for a library file depends on its location. It is
  34. also problematic for projects trying to link statically because flags
  35. like ``-Wl,-Bstatic -lfoo -Wl,-Bdynamic`` may be used to help the linker
  36. select ``libfoo.a`` instead of ``libfoo.so`` but then leak dynamic linking
  37. to following libraries. (See the :prop_tgt:`LINK_SEARCH_END_STATIC`
  38. target property for a solution typically used for that problem.)
  39. When the special case for libraries in implicit link directories was first
  40. introduced the list of implicit link directories was simply hard-coded
  41. (e.g. ``/lib``, ``/usr/lib``, and a few others). Since that time, CMake
  42. has learned to detect the implicit link directories used by the compiler
  43. front-end. If necessary, the :command:`find_library` command could be
  44. taught to use this information to help find libraries of the proper
  45. architecture.
  46. For these reasons, CMake 3.3 and above prefer to drop the special case
  47. and link libraries by full path even when they are in implicit link
  48. directories. Policy ``CMP0060`` provides compatibility for existing
  49. projects.
  50. The ``OLD`` behavior for this policy is to ask the linker to search for
  51. libraries whose full paths are known to be in implicit link directories.
  52. The ``NEW`` behavior for this policy is to link libraries by full path even
  53. if they are in implicit link directories.
  54. .. |INTRODUCED_IN_CMAKE_VERSION| replace:: 3.3
  55. .. |WARNED_OR_DID_NOT_WARN| replace:: did *not* warn by default
  56. .. include:: include/REMOVED_EPILOGUE.rst
  57. See documentation of the
  58. :variable:`CMAKE_POLICY_WARNING_CMP0060 <CMAKE_POLICY_WARNING_CMP<NNNN>>`
  59. variable to control the warning in CMake versions before 4.0.