Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-88935

Advocate for using explicit linkage in target_link_libraries

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Priority: P3: Somewhat important
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Documentation
    • Labels:
      None
    • Technical Risk:
      Normal

      Description

      So far Qt has been using the target_link_libraries overload that doesn't specify any linkage, e.g.

      target_link_libraries(mytarget Qt6::Core)
      

      Anyhow, the Professional CMake book suggests always using explicit linkage with
      target_link_libraries. In particular, it reinforces the habit and forces you to think about the relationships between targets. The type of linkage you get if you don't specify it actually depends on a policy setting.

      The actual type of linkage (PUBLIC, PRIVATE, INTERFACE) to be used depends very much on the use case. Whether to use PUBLIC or PRIVATE for Qt libraries depends on whether something that consumes mytarget needs to link to Qt6::Test as well. In practice, this usually means use PUBLIC if anything from Qt6::Test is mentioned in a header of mytarget, use PRIVATE otherwise.

      You should always prefer PRIVATE where you can to minimise the internal dependencies you expose out to consumers.

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            kkohne Kai Köhne
            Reporter:
            kkohne Kai Köhne
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: