Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. CMake for static library using Qt Core
Forum Update on Monday, May 27th 2025

CMake for static library using Qt Core

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CodeFan
    wrote on 5 Feb 2024, 05:44 last edited by
    #1

    I have a project that uses CMake and builds just fine. I want to pull a single class out into a re-useable static library. The class does use QFile and QString. I thought I had my CMakeLists.txt setup correctly with the find_packages(Qt6 Components Core Required). But, when I try to build, the compile of the class source can't find QFile or other Qt symbols. Below is a minimum example I created to show the same issue. Any idea what I have wrong or missing?

    CMakeLists.txt:

    cmake_minimum_required(VERSION 3.16)
    
    project(qt_testlib LANGUAGES CXX)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(Qt6 COMPONENTS Core REQUIRED)
    
    add_library(qt_testlib STATIC
      MyClass.cpp
      MyClass.h
    )
    
    target_link_libraries(qt_testlib PRIVATE)
    
    target_compile_definitions(qt_testlib PRIVATE COMMON_LIBRARY)
    

    Class header file:

    #ifndef MYCLASS_H_INCLUDED
    #define MYCLASS_H_INCLUDED
    
    #include <string>
    
    namespace testlib {
    
    class MyClass
    {
    public:
        static std::string Load(const std::string& path);
    };
    
    } // namespace testlib
    
    #endif // MYCLASS_H_INCLUDED
    

    Class source file:

    #include "MyClass.h"
    
    #include <QFile>
    #include <QString>
    #include <QTextStream>
    
    namespace testlib {
    
    std::string MyClass::Load(const std::string& path)
    {
        QString qpath(":/");
        qpath += path.c_str();
        QFile file(qpath);
        if (!file.open(QIODeviceBase::ReadOnly | QIODeviceBase::Text))
        {
            return "";
        }
    
        std::string returnVal;
        QTextStream textStream(&file);
        if (!textStream.atEnd())
        {
            returnVal = textStream.readLine().toStdString();
        }
    
        return returnVal;
    }
    
    } // namespace testlib
    
    J 1 Reply Last reply 5 Feb 2024, 06:18
    0
    • C CodeFan
      5 Feb 2024, 05:44

      I have a project that uses CMake and builds just fine. I want to pull a single class out into a re-useable static library. The class does use QFile and QString. I thought I had my CMakeLists.txt setup correctly with the find_packages(Qt6 Components Core Required). But, when I try to build, the compile of the class source can't find QFile or other Qt symbols. Below is a minimum example I created to show the same issue. Any idea what I have wrong or missing?

      CMakeLists.txt:

      cmake_minimum_required(VERSION 3.16)
      
      project(qt_testlib LANGUAGES CXX)
      
      set(CMAKE_INCLUDE_CURRENT_DIR ON)
      set(CMAKE_CXX_STANDARD 17)
      set(CMAKE_CXX_STANDARD_REQUIRED ON)
      
      find_package(Qt6 COMPONENTS Core REQUIRED)
      
      add_library(qt_testlib STATIC
        MyClass.cpp
        MyClass.h
      )
      
      target_link_libraries(qt_testlib PRIVATE)
      
      target_compile_definitions(qt_testlib PRIVATE COMMON_LIBRARY)
      

      Class header file:

      #ifndef MYCLASS_H_INCLUDED
      #define MYCLASS_H_INCLUDED
      
      #include <string>
      
      namespace testlib {
      
      class MyClass
      {
      public:
          static std::string Load(const std::string& path);
      };
      
      } // namespace testlib
      
      #endif // MYCLASS_H_INCLUDED
      

      Class source file:

      #include "MyClass.h"
      
      #include <QFile>
      #include <QString>
      #include <QTextStream>
      
      namespace testlib {
      
      std::string MyClass::Load(const std::string& path)
      {
          QString qpath(":/");
          qpath += path.c_str();
          QFile file(qpath);
          if (!file.open(QIODeviceBase::ReadOnly | QIODeviceBase::Text))
          {
              return "";
          }
      
          std::string returnVal;
          QTextStream textStream(&file);
          if (!textStream.atEnd())
          {
              returnVal = textStream.readLine().toStdString();
          }
      
          return returnVal;
      }
      
      } // namespace testlib
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 5 Feb 2024, 06:18 last edited by
      #2

      @CodeFan You're missing

      target_link_libraries(qt_testlib PRIVATE Qt6::Core)
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply 5 Feb 2024, 18:45
      2
      • J jsulm
        5 Feb 2024, 06:18

        @CodeFan You're missing

        target_link_libraries(qt_testlib PRIVATE Qt6::Core)
        
        C Offline
        C Offline
        CodeFan
        wrote on 5 Feb 2024, 18:45 last edited by
        #3

        @jsulm Thank you for the quick reply. Adding Qt6::Core to the target_link_libraries statement does allow the build to succeed. But, isn't that telling CMake to statically link Qt6::Core into my static lbrary? Meaning, parts of Qt6::Core implementation will be included in my library as well? It seems odd to have to specify a link library in order for a #include to be found at compile time. With that change, the resulting qt_testlib.a is over 500k in size for one tiny class. The 'nm' tool shows lots of Qt function definitions included in the library and not just templated ones. I would have expected these non-templated ones to be undefined and only reside in the Qt library.

        SGaistS 1 Reply Last reply 5 Feb 2024, 19:59
        0
        • C CodeFan
          5 Feb 2024, 18:45

          @jsulm Thank you for the quick reply. Adding Qt6::Core to the target_link_libraries statement does allow the build to succeed. But, isn't that telling CMake to statically link Qt6::Core into my static lbrary? Meaning, parts of Qt6::Core implementation will be included in my library as well? It seems odd to have to specify a link library in order for a #include to be found at compile time. With that change, the resulting qt_testlib.a is over 500k in size for one tiny class. The 'nm' tool shows lots of Qt function definitions included in the library and not just templated ones. I would have expected these non-templated ones to be undefined and only reside in the Qt library.

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 5 Feb 2024, 19:59 last edited by
          #4

          @CodeFan hi,

          No it does not. The use of PRIVATE means that the library you are linking is only used internally and users of your code won't need to link to that library.

          As for needing target_link_libraries, find_package just finds the dependencies, it does not tell the system what to do with them.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 2 Replies Last reply 6 Feb 2024, 00:15
          1
          • SGaistS SGaist
            5 Feb 2024, 19:59

            @CodeFan hi,

            No it does not. The use of PRIVATE means that the library you are linking is only used internally and users of your code won't need to link to that library.

            As for needing target_link_libraries, find_package just finds the dependencies, it does not tell the system what to do with them.

            C Offline
            C Offline
            CodeFan
            wrote on 6 Feb 2024, 00:15 last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • SGaistS SGaist
              5 Feb 2024, 19:59

              @CodeFan hi,

              No it does not. The use of PRIVATE means that the library you are linking is only used internally and users of your code won't need to link to that library.

              As for needing target_link_libraries, find_package just finds the dependencies, it does not tell the system what to do with them.

              C Offline
              C Offline
              CodeFan
              wrote on 6 Feb 2024, 02:54 last edited by
              #6

              @SGaist I re-read the CMake docs. It certainly agrees with what you said. I guess the part that confuses me is why my compiled libqt_testlib.a has definitions for many Qt symbols.

              nm -C --defined-only libqt_testlib.a gives output including these:
              QArrayData::deref()
              QByteArray::~QByteArray()
              QByteArrayView::castHelper(char const*)
              QFlag::QFlag(unsigned int)
              QString::QString(char const*)
              QString::~QString()
              QString::operator+=(char const*)
              QFlag::operator unsigned int() const
              QString::toStdStringabi:cxx11 const
              QString::toUtf8() const &

              Christian EhrlicherC 1 Reply Last reply 6 Feb 2024, 05:16
              0
              • C CodeFan
                6 Feb 2024, 02:54

                @SGaist I re-read the CMake docs. It certainly agrees with what you said. I guess the part that confuses me is why my compiled libqt_testlib.a has definitions for many Qt symbols.

                nm -C --defined-only libqt_testlib.a gives output including these:
                QArrayData::deref()
                QByteArray::~QByteArray()
                QByteArrayView::castHelper(char const*)
                QFlag::QFlag(unsigned int)
                QString::QString(char const*)
                QString::~QString()
                QString::operator+=(char const*)
                QFlag::operator unsigned int() const
                QString::toStdStringabi:cxx11 const
                QString::toUtf8() const &

                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 6 Feb 2024, 05:16 last edited by
                #7

                @CodeFan These are all inlined functions.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  SimonSchroeder
                  wrote on 6 Feb 2024, 07:59 last edited by
                  #8

                  With static linking you can be sure that final linking of the executable will only include the parts of the library that are actually used. Don't bother to much with the size of your own library (though most likely you really have that many dependencies because of the features you are using from QtCore).

                  C 1 Reply Last reply 6 Feb 2024, 18:51
                  0
                  • S SimonSchroeder
                    6 Feb 2024, 07:59

                    With static linking you can be sure that final linking of the executable will only include the parts of the library that are actually used. Don't bother to much with the size of your own library (though most likely you really have that many dependencies because of the features you are using from QtCore).

                    C Offline
                    C Offline
                    CodeFan
                    wrote on 6 Feb 2024, 18:51 last edited by
                    #9

                    Thanks to all who contributed! I really appreciate it.

                    1 Reply Last reply
                    0
                    • C CodeFan has marked this topic as solved on 6 Feb 2024, 18:51

                    1/9

                    5 Feb 2024, 05:44

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved