How To Link Libraries Cmake Mac

CMake之findpackage 首先强烈推荐对CMake不熟的同学先看这本书《Cmake实践》。 CMake说起来是个好东西,可是真正用的时候并不那么容易,很容易出现各种各样的错误。这不,最近就被findpackage这个命令折腾得死去活来。. Describe the bug linking to pcre2 fails using qt on mac in Debug config. In Release config libpq doesn't link correctly. Environment OS: Mac OS X 10.15 Compiler: Clang 11.0.0 To Reproduce link to Qt in CMake build the executable with CMa.

1

I have a project that includes a prebuilt version of opencv in a subdirectory. For example:

How To Link Libraries Cmake Mac

MyProject
* CMakeLists.txt
* src
* third_party
** CMakeLists.txt
** opencv
**** include
**** lib

I would like to link against the version of opencv located in the third_party directory. My question is, how do I inform CMake to link to the prebuilt dylib files in lib, and include the headers in the relevant opencv directory?

1 answers

4

I've given your example a try with CMake 3.3.2 on OS X 10.11 having XCode 7.0.1.

Using the link_directories() and target_link_libraries() approach suggested by @Tsyvarev seems to work without raising any linker warnings or errors (it finds the .dylib libraries I placed in the third_party directory).

Just a view hints, that hopefully could get you a start why it's not working on your Mac.

  • With your code I get the following command line linker file (inside CMake's binary output directory):

    CMakeFiles/myproject.dir/src/link.txt

  • You can try to give full library paths, because those are additionally checked by CMake itself and it gets more obvious what I link against. Here is a modified version of your example:

    CMakeLists.txt

    With this CMake just adds fully qualified paths (relative to my binary output directory) into the linker file. The -L and -l options are gone and you get 'lines' like:

Additional Q/A References

Contents

  • target_link_libraries

Specify libraries or flags to use when linking a given target and/orits dependents. Usage requirementsfrom linked library targets will be propagated. Usage requirementsof a target’s dependencies affect compilation of its own sources.

This command has several signatures as detailed in subsections below.All of them have the general form:

Download

The named <target> must have been created in the current directory bya command such as add_executable() or add_library().Repeated calls for the same <target> append items in the order called.Each <item> may be:

  • A library target name: The generated link line will have thefull path to the linkable library file associated with the target.The buildsystem will have a dependency to re-link <target> ifthe library file changes.

    The named target must be created by add_library() withinthe project or as an IMPORTED library.If it is created within the project an ordering dependency willautomatically be added in the build system to make sure the namedlibrary target is up-to-date before the <target> links.

    If an imported library has the IMPORTED_NO_SONAMEtarget property set, CMake may ask the linker to search forthe library instead of using the full path(e.g. /usr/lib/libfoo.so becomes -lfoo).

  • A full path to a library file: The generated link line willnormally preserve the full path to the file. The buildsystem willhave a dependency to re-link <target> if the library file changes.

    There are some cases where CMake may ask the linker to search forthe library (e.g. /usr/lib/libfoo.so becomes -lfoo), suchas when a shared library is detected to have no SONAME field.See policy CMP0060 for discussion of another case.

    If the library file is in a Mac OSX framework, the Headers directoryof the framework will also be processed as ausage requirement. This has the sameeffect as passing the framework directory as an include directory.

  • A plain library name: The generated link line will ask the linkerto search for the library (e.g. foo becomes -lfoo or foo.lib).

  • A link flag: Item names starting with -, but not -l or-framework, are treated as linker flags. Note that such flags willbe treated like any other library link item for purposes of transitivedependencies, so they are generally safe to specify only as private linkitems that will not propagate to dependents.

  • A debug, optimized, or general keyword immediately followedby another <item>. The item following such a keyword will be usedonly for the corresponding build configuration. The debug keywordcorresponds to the Debug configuration (or to configurations namedin the DEBUG_CONFIGURATIONS global property if it is set).The optimized keyword corresponds to all other configurations. Thegeneral keyword corresponds to all configurations, and is purelyoptional. Higher granularity may be achieved for per-configurationrules by creating and linking toIMPORTED library targets.

Items containing ::, such as Foo::Bar, are assumed to beIMPORTED or ALIAS librarytarget names and will cause an error if no such target exists.See policy CMP0028.

Arguments to target_link_libraries may use “generator expressions”with the syntax $<..>. Note however, that generator expressionswill not be used in OLD handling of CMP0003 or CMP0004.See the cmake-generator-expressions(7) manual for availableexpressions. See the cmake-buildsystem(7) manual for more ondefining buildsystem properties.

The PUBLIC, PRIVATE and INTERFACE keywords can be used tospecify both the link dependencies and the link interface in one command.Libraries and targets following PUBLIC are linked to, and are madepart of the link interface. Libraries and targets following PRIVATEare linked to, but are not made part of the link interface. Librariesfollowing INTERFACE are appended to the link interface and are notused for linking <target>.

Library dependencies are transitive by default with this signature.When this target is linked into another target then the librarieslinked to this target will appear on the link line for the othertarget too. This transitive “link interface” is stored in theINTERFACE_LINK_LIBRARIES target property and may be overriddenby setting the property directly. When CMP0022 is not set toNEW, transitive linking is built in but may be overridden by theLINK_INTERFACE_LIBRARIES property. Calls to other signaturesof this command may set the property making any libraries linkedexclusively by this signature private.

The LINK_PUBLIC and LINK_PRIVATE modes can be used to specify boththe link dependencies and the link interface in one command.

This signature is for compatibility only. Prefer the PUBLIC orPRIVATE keywords instead.

Libraries and targets following LINK_PUBLIC are linked to, and aremade part of the INTERFACE_LINK_LIBRARIES. If policyCMP0022 is not NEW, they are also made part of theLINK_INTERFACE_LIBRARIES. Libraries and targets followingLINK_PRIVATE are linked to, but are not made part of theINTERFACE_LINK_LIBRARIES (or LINK_INTERFACE_LIBRARIES).

The LINK_INTERFACE_LIBRARIES mode appends the libraries to theINTERFACE_LINK_LIBRARIES target property instead of using themfor linking. If policy CMP0022 is not NEW, then this modealso appends libraries to the LINK_INTERFACE_LIBRARIES and itsper-configuration equivalent.

This signature is for compatibility only. Prefer the INTERFACE modeinstead.

Cmake Add Library

Libraries specified as debug are wrapped in a generator expression tocorrespond to debug builds. If policy CMP0022 isnot NEW, the libraries are also appended to the'>LINK_INTERFACE_LIBRARIES_DEBUGproperty (or to the properties corresponding to configurations listed inthe DEBUG_CONFIGURATIONS global property if it is set).Libraries specified as optimized are appended to theINTERFACE_LINK_LIBRARIES property. If policy CMP0022is not NEW, they are also appended to theLINK_INTERFACE_LIBRARIES property. Libraries specified asgeneral (or without any keyword) are treated as if specified for bothdebug and optimized.

The library dependency graph is normally acyclic (a DAG), but in the caseof mutually-dependent STATIC libraries CMake allows the graph tocontain cycles (strongly connected components). When another target linksto one of the libraries, CMake repeats the entire connected component.For example, the code

links main to ABAB. While one repetition is usuallysufficient, pathological object file and symbol arrangements can requiremore. One may handle such cases by using theLINK_INTERFACE_MULTIPLICITY target property or by manuallyrepeating the component in the last target_link_libraries call.However, if two archives are really so interdependent they should probablybe combined into a single archive, perhaps by using Object Libraries.

Note that it is not advisable to populate theINTERFACE_LINK_LIBRARIES of a target with absolute paths to dependencies.That would hard-code into installed packages the library file pathsfor dependencies as found on the machine the package was made on.

If you prefer, you can store photos and videos outside the Photos library (for example, in a folder on your Mac or on an external drive) and still view them in Photos. Change disc location of photo library on mac and cheese

See the Creating Relocatable Packages section of thecmake-packages(7) manual for discussion of additional carethat must be taken when specifying usage requirements while creatingpackages for redistribution.