link_directories.rst 2.4 KB

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