Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. build my own library with Cmake in Qt6
Qt 6.12 LTS is out! See what's new in the release blog

build my own library with Cmake in Qt6

Scheduled Pinned Locked Moved Unsolved Language Bindings
11 Posts 2 Posters 5.4k 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.
  • W withwind

    Hallo everyone, i try to build my own libary and try to reuse it, but the setting in Cmake seems not working, my include of the built libray is not recognized by system,

    do have anyone a general tutorial or tell me now how can man build his own library with Cmake in Qt6 and can be set to reuse them in programm? Thanks a lot.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @withwind If I understood you correctly you are able to build the lib, but you can't use it.
    So, how do you add this lib to your project (show your CMakeLists.txt)?

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

    W 1 Reply Last reply
    0
    • W Offline
      W Offline
      withwind
      wrote on last edited by
      #3

      thanks for answer,

      i have tried to build a small dynamish linked libary called mylib, it is built with Cmake and i got the libmylib.dll and libmylib.dll.a, i try to reuse it in my another program, i include it like "#include "mylib"" but it is not recognized,showing "no such file or directory".

      I dont know how to make it recognized. Maybe some path setting in CMakeList.text.

      I try to set some path in CMakeText.text, but maybe the setting is not correct. Many tutorials are showd with Qmake, it help litte.my cmake text:

      cmake_minimum_required(VERSION 3.14)

      project(libPing LANGUAGES CXX)

      set(CMAKE_INCLUDE_CURRENT_DIR ON)
      set(CMAKE_AUTOUIC ON)
      set(CMAKE_AUTOMOC ON)
      set(CMAKE_AUTORCC ON)
      set(CMAKE_CXX_STANDARD 17)
      set(CMAKE_CXX_STANDARD_REQUIRED ON)

      find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
      find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)

      add_library(libPing SHARED
      libPing_global.h
      libping.cpp
      libping.h
      )

      target_link_libraries(libPing PRIVATE Qt${QT_VERSION_MAJOR}::Core)

      target_compile_definitions(libPing PRIVATE LIBPING_LIBRARY)

      target_include_directories(libPing INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

      1 Reply Last reply
      0
      • jsulmJ jsulm

        @withwind If I understood you correctly you are able to build the lib, but you can't use it.
        So, how do you add this lib to your project (show your CMakeLists.txt)?

        W Offline
        W Offline
        withwind
        wrote on last edited by withwind
        #4

        @jsulm

        thanks for answer,

        i have tried to build a small dynamish linked libary called libPing, it is built with Cmake and i got the liblibPing.dll and liblibPing.dll.a, i try to reuse it in my another program, i include it like "#include "libPing"" but it is not recognized,showing "no such file or directory".

        I dont know how to make it recognized. Maybe some path setting in CMakeList.text.

        I try to set some path in CMakeText.text, but maybe the setting is not correct. Many tutorials are showd with Qmake, it help litte.my cmake text:
        cmake_minimum_required(VERSION 3.14)

        project(libPing LANGUAGES CXX)

        set(CMAKE_INCLUDE_CURRENT_DIR ON)
        set(CMAKE_AUTOUIC ON)
        set(CMAKE_AUTOMOC ON)
        set(CMAKE_AUTORCC ON)
        set(CMAKE_CXX_STANDARD 17)
        set(CMAKE_CXX_STANDARD_REQUIRED ON)

        find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
        find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)

        add_library(libPing SHARED
        libPing_global.h
        libping.cpp
        libping.h
        )

        target_link_libraries(libPing PRIVATE Qt${QT_VERSION_MAJOR}::Core)

        target_compile_definitions(libPing PRIVATE LIBPING_LIBRARY)

        target_include_directories(libPing INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

        jsulmJ 1 Reply Last reply
        0
        • W withwind

          @jsulm

          thanks for answer,

          i have tried to build a small dynamish linked libary called libPing, it is built with Cmake and i got the liblibPing.dll and liblibPing.dll.a, i try to reuse it in my another program, i include it like "#include "libPing"" but it is not recognized,showing "no such file or directory".

          I dont know how to make it recognized. Maybe some path setting in CMakeList.text.

          I try to set some path in CMakeText.text, but maybe the setting is not correct. Many tutorials are showd with Qmake, it help litte.my cmake text:
          cmake_minimum_required(VERSION 3.14)

          project(libPing LANGUAGES CXX)

          set(CMAKE_INCLUDE_CURRENT_DIR ON)
          set(CMAKE_AUTOUIC ON)
          set(CMAKE_AUTOMOC ON)
          set(CMAKE_AUTORCC ON)
          set(CMAKE_CXX_STANDARD 17)
          set(CMAKE_CXX_STANDARD_REQUIRED ON)

          find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
          find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)

          add_library(libPing SHARED
          libPing_global.h
          libping.cpp
          libping.h
          )

          target_link_libraries(libPing PRIVATE Qt${QT_VERSION_MAJOR}::Core)

          target_compile_definitions(libPing PRIVATE LIBPING_LIBRARY)

          target_include_directories(libPing INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @withwind I wanted to see your CMakeLists.txt where you are ADDING your library to the project, not the one to build library itself. So, please show CMakeLists.txt from the project which USES the library. In that CMakeLists.txt you need to link your library to your project using https://cmake.org/cmake/help/latest/command/target_link_libraries.html

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

          W 1 Reply Last reply
          0
          • jsulmJ jsulm

            @withwind I wanted to see your CMakeLists.txt where you are ADDING your library to the project, not the one to build library itself. So, please show CMakeLists.txt from the project which USES the library. In that CMakeLists.txt you need to link your library to your project using https://cmake.org/cmake/help/latest/command/target_link_libraries.html

            W Offline
            W Offline
            withwind
            wrote on last edited by
            #6

            @jsulm
            thanks again!
            that means, for using the custom library, man have to set the CMakeLists.txt in the project. The CMakeLists.txt is:

            cmake_minimum_required(VERSION 3.14)

            project(demoPing LANGUAGES CXX)

            set(CMAKE_INCLUDE_CURRENT_DIR ON)

            set(CMAKE_AUTOUIC ON)
            set(CMAKE_AUTOMOC ON)
            set(CMAKE_AUTORCC ON)

            set(CMAKE_CXX_STANDARD 17)
            set(CMAKE_CXX_STANDARD_REQUIRED ON)

            find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
            find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)

            add_executable(demoPing
            main.cpp
            )
            target_link_libraries(demoPing Qt${QT_VERSION_MAJOR}::Core)

            install(TARGETS demoPing
            LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

            jsulmJ 1 Reply Last reply
            0
            • W withwind

              @jsulm
              thanks again!
              that means, for using the custom library, man have to set the CMakeLists.txt in the project. The CMakeLists.txt is:

              cmake_minimum_required(VERSION 3.14)

              project(demoPing LANGUAGES CXX)

              set(CMAKE_INCLUDE_CURRENT_DIR ON)

              set(CMAKE_AUTOUIC ON)
              set(CMAKE_AUTOMOC ON)
              set(CMAKE_AUTORCC ON)

              set(CMAKE_CXX_STANDARD 17)
              set(CMAKE_CXX_STANDARD_REQUIRED ON)

              find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
              find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)

              add_executable(demoPing
              main.cpp
              )
              target_link_libraries(demoPing Qt${QT_VERSION_MAJOR}::Core)

              install(TARGETS demoPing
              LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @withwind said in build my own library with Cmake in Qt6:

              target_link_libraries(demoPing Qt${QT_VERSION_MAJOR}::Core)

              No wonder it does not work: you do not link your lib to your executable...

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

              W 1 Reply Last reply
              0
              • jsulmJ jsulm

                @withwind said in build my own library with Cmake in Qt6:

                target_link_libraries(demoPing Qt${QT_VERSION_MAJOR}::Core)

                No wonder it does not work: you do not link your lib to your executable...

                W Offline
                W Offline
                withwind
                wrote on last edited by
                #8

                @jsulm
                Can you can tell me how to link my lib to my project? thanks

                jsulmJ 1 Reply Last reply
                0
                • W withwind

                  @jsulm
                  Can you can tell me how to link my lib to my project? thanks

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @withwind I already did above: "you need to link your library to your project using https://cmake.org/cmake/help/latest/command/target_link_libraries.html"

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

                  W 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @withwind I already did above: "you need to link your library to your project using https://cmake.org/cmake/help/latest/command/target_link_libraries.html"

                    W Offline
                    W Offline
                    withwind
                    wrote on last edited by
                    #10

                    @jsulm
                    i have added this in my CMake file in project:

                    add_library(libPing SHARED ../libPing/libping.h)
                    target_link_libraries(libPing PRIVATE Qt6::Core)

                    but it doesn't work

                    can you give me some instructions? thanks

                    jsulmJ 1 Reply Last reply
                    0
                    • W withwind

                      @jsulm
                      i have added this in my CMake file in project:

                      add_library(libPing SHARED ../libPing/libping.h)
                      target_link_libraries(libPing PRIVATE Qt6::Core)

                      but it doesn't work

                      can you give me some instructions? thanks

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @withwind You need to have something like

                      target_link_libraries(demoPing libPing...
                      

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

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt
                      • Unsolved