CMake target_link_libraries Interface Dependencies

后端 未结 3 481
攒了一身酷
攒了一身酷 2020-12-07 07:46

I am new to CMake and a bit confused with the PUBLIC, PRIVATE and INTERFACE keywords related to target_link_libraries(). Documentation mentions that they can be used to spec

相关标签:
3条回答
  • 2020-12-07 08:12

    Some answers only said when to use PRIVATE/PUBLIC/INTERFACE, but the affects are ignored. Refer:CMake-Public-Private-Interface

    PUBLIC
    All the objects following PUBLIC will be used for linking to the current target and providing the interface to the other targets that have dependencies on the current target.

    PRIVATE
    All the objects following PRIVATE will only be used for linking to the current target.

    INTERFACE
    All the objects following INTERFACE will only be used for providing the interface to the other targets that have dependencies on the current target.

    0 讨论(0)
  • 2020-12-07 08:25

    If you are creating a shared library and your source cpp files #include the headers of another library (Say, QtNetwork for example), but your header files don't include QtNetwork headers, then QtNetwork is a PRIVATE dependency.

    If your source files and your headers include the headers of another library, then it is a PUBLIC dependency.

    If your header files but not your source files include the headers of another library, then it is an INTERFACE dependency.

    Other build properties of PUBLIC and INTERFACE dependencies are propagated to consuming libraries. http://www.cmake.org/cmake/help/v3.0/manual/cmake-buildsystem.7.html#transitive-usage-requirements

    0 讨论(0)
  • 2020-12-07 08:34

    @steveire accepted answer is great. I just wanted to add a table to quickly see the difference:

    .-----------.------------------.----------------.
    |           | Linked by target | Link interface |
    :-----------+------------------+----------------:
    | PUBLIC    |        X         |        X       |
    :-----------+------------------+----------------:
    | PRIVATE   |        X         |                |
    :-----------+------------------+----------------:
    | INTERFACE |                  |        X       |
    '-----------'------------------'----------------'
    
    • Linked by target: libraries included in target sources (not a dependency for projects linking the library).
    • Link interface: libraries included in target public headers (dependencies for projects linking the library).
    0 讨论(0)
提交回复
热议问题