link_directories.rst 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. link_directories
  2. ----------------
  3. Add directories in which the linker will look for libraries.
  4. .. code-block:: cmake
  5. link_directories([AFTER|BEFORE] directory1 [directory2 ...])
  6. Adds the paths in which the linker should search for libraries.
  7. Relative paths given to this command are interpreted as relative to
  8. the current source directory, see :policy:`CMP0015`.
  9. The command will apply only to targets created after it is called.
  10. .. versionadded:: 3.13
  11. The directories are added to the :prop_dir:`LINK_DIRECTORIES` directory
  12. property for the current ``CMakeLists.txt`` file, converting relative
  13. paths to absolute as needed. See the :manual:`cmake-buildsystem(7)`
  14. manual for more on defining buildsystem properties.
  15. .. versionadded:: 3.13
  16. By default the directories specified are appended onto the current list of
  17. directories. This default behavior can be changed by setting
  18. :variable:`CMAKE_LINK_DIRECTORIES_BEFORE` to ``ON``. By using
  19. ``AFTER`` or ``BEFORE`` explicitly, you can select between appending and
  20. prepending, independent of the default.
  21. .. versionadded:: 3.13
  22. Arguments to ``link_directories`` may use "generator expressions" with
  23. the syntax "$<...>". See the :manual:`cmake-generator-expressions(7)`
  24. manual for available expressions.
  25. .. note::
  26. This command is rarely necessary and should be avoided where there are
  27. other choices. Prefer to pass full absolute paths to libraries where
  28. possible, since this ensures the correct library will always be linked.
  29. The :command:`find_library` command provides the full path, which can
  30. generally be used directly in calls to :command:`target_link_libraries`.
  31. Situations where a library search path may be needed include:
  32. - Project generators like Xcode where the user can switch target
  33. architecture at build time, but a full path to a library cannot
  34. be used because it only provides one architecture (i.e. it is not
  35. a universal binary).
  36. - Libraries may themselves have other private library dependencies
  37. that expect to be found via ``RPATH`` mechanisms, but some linkers
  38. are not able to fully decode those paths (e.g. due to the presence
  39. of things like ``$ORIGIN``).
  40. If a library search path must be provided, prefer to localize the effect
  41. where possible by using the :command:`target_link_directories` command
  42. rather than ``link_directories()``. The target-specific command can also
  43. control how the search directories propagate to other dependent targets.
  44. See Also
  45. ^^^^^^^^
  46. * :command:`target_link_directories`
  47. * :command:`target_link_libraries`