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 with QT library
Forum Updated to NodeBB v4.3 + New Features

CMAKE with QT library

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.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.
  • Q Offline
    Q Offline
    qjqj
    wrote on last edited by
    #1

    Hi,

    I am writing a C++ program using Eclipse CDT and I want to include QT library into my program. However, I only want specify library e.g libqt5core, libqt5network... and not the entire library file. In my cmakelist.txt I have include those path and link the library. But when I make it shows:

    undefined reference to `vtable ....`
    

    I did some googling and most of them state that I need to find the package of the library. However, I want my program to be able to port over to any computer without the need to install qt. Hence, I am trying to put all the library and header file that I need into one project folder.

    I am using linux ubuntu 16.04 with qt5.8.

    Below is my cmakelist.txt:

    cmake_minimum_required(VERSION 2.8)
    
    project(testing)
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIC")
    
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    #
    #MESSAGE(STATUS "CMAKE_CURRENT_SOURCE_DIR" ${CMAKE_CURRENT_SOURCE_DIR})
    file(GLOB_RECURSE Qt_lib "${CMAKE_CURRENT_SOURCE_DIR}/lib/*.so")
    file(GLOB src_files "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
    file(GLOB src "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
    #
    set(SOURCES ${src_files}
    ${src}
    )
    
    include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}/include/
    ${CMAKE_CURRENT_SOURCE_DIR}/include/QtGui
    ${CMAKE_CURRENT_SOURCE_DIR}/include/QtWidgets
    )
    
    
    add_executable(${PROJECT_NAME} 
    ${SOURCES}
    ${HEADER_FILES}
    )
    
    target_link_libraries(${PROJECT_NAME}
    ${Qt_lib}
    )
    
    VRoninV 1 Reply Last reply
    0
    • Q qjqj

      Hi,

      I am writing a C++ program using Eclipse CDT and I want to include QT library into my program. However, I only want specify library e.g libqt5core, libqt5network... and not the entire library file. In my cmakelist.txt I have include those path and link the library. But when I make it shows:

      undefined reference to `vtable ....`
      

      I did some googling and most of them state that I need to find the package of the library. However, I want my program to be able to port over to any computer without the need to install qt. Hence, I am trying to put all the library and header file that I need into one project folder.

      I am using linux ubuntu 16.04 with qt5.8.

      Below is my cmakelist.txt:

      cmake_minimum_required(VERSION 2.8)
      
      project(testing)
      
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIC")
      
      set(CMAKE_AUTOMOC ON)
      set(CMAKE_INCLUDE_CURRENT_DIR ON)
      
      #
      #MESSAGE(STATUS "CMAKE_CURRENT_SOURCE_DIR" ${CMAKE_CURRENT_SOURCE_DIR})
      file(GLOB_RECURSE Qt_lib "${CMAKE_CURRENT_SOURCE_DIR}/lib/*.so")
      file(GLOB src_files "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
      file(GLOB src "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
      #
      set(SOURCES ${src_files}
      ${src}
      )
      
      include_directories(
      ${CMAKE_CURRENT_SOURCE_DIR}
      ${CMAKE_CURRENT_SOURCE_DIR}/src
      ${CMAKE_CURRENT_SOURCE_DIR}/include/
      ${CMAKE_CURRENT_SOURCE_DIR}/include/QtGui
      ${CMAKE_CURRENT_SOURCE_DIR}/include/QtWidgets
      )
      
      
      add_executable(${PROJECT_NAME} 
      ${SOURCES}
      ${HEADER_FILES}
      )
      
      target_link_libraries(${PROJECT_NAME}
      ${Qt_lib}
      )
      
      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      This is a clear example of how we need a better documentation for CMake in Qt.

      Your project file should be:

      cmake_minimum_required(VERSION 3.3)
      project(testing)
      set(CMAKE_CXX_STANDARD 11)
      set(CMAKE_CXX_STANDARD_REQUIRED ON)
      set(REQUIRED_QT_VERSION 5.0.0)
      find_package(Qt5Core ${REQUIRED_QT_VERSION} REQUIRED)
      find_package(Qt5Gui ${REQUIRED_QT_VERSION} REQUIRED)
      find_package(Qt5Widgets ${REQUIRED_QT_VERSION} REQUIRED)
      set(Qt_INCLUDE ${Qt5Core_INCLUDE_DIRS} ${Qt5Gui_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS})
      set(Qt_LIBS ${Qt5Core_LIBRARIES} ${Qt5Gui_LIBRARIES} ${Qt5Widgets_LIBRARIES})
      set(Qt_COMPILE_DEFINE ${Qt5Core_COMPILE_DEFINITIONS} ${Qt5Gui_COMPILE_DEFINITIONS} ${Qt5Widgets_COMPILE_DEFINITIONS})
      set(CMAKE_AUTOMOC ON)
      set(CMAKE_INCLUDE_CURRENT_DIR ON)
      file(GLOB src_files "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
      file(GLOB src "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
      set(SOURCES ${src_files} ${src})
      add_executable(${PROJECT_NAME} ${SOURCES})
      target_include_directories(${PROJECT_NAME} PUBLIC ${Qt_INCLUDE} "${CMAKE_CURRENT_SOURCE_DIR}/src")
      target_link_libraries(${PROJECT_NAME} PUBLIC ${Qt_LIBS})
      target_compile_definitions(${PROJECT_NAME} PRIVATE ${Qt_COMPILE_DEFINE})
      set_property(TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      Q 1 Reply Last reply
      2
      • Q Offline
        Q Offline
        qjqj
        wrote on last edited by qjqj
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • VRoninV VRonin

          This is a clear example of how we need a better documentation for CMake in Qt.

          Your project file should be:

          cmake_minimum_required(VERSION 3.3)
          project(testing)
          set(CMAKE_CXX_STANDARD 11)
          set(CMAKE_CXX_STANDARD_REQUIRED ON)
          set(REQUIRED_QT_VERSION 5.0.0)
          find_package(Qt5Core ${REQUIRED_QT_VERSION} REQUIRED)
          find_package(Qt5Gui ${REQUIRED_QT_VERSION} REQUIRED)
          find_package(Qt5Widgets ${REQUIRED_QT_VERSION} REQUIRED)
          set(Qt_INCLUDE ${Qt5Core_INCLUDE_DIRS} ${Qt5Gui_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS})
          set(Qt_LIBS ${Qt5Core_LIBRARIES} ${Qt5Gui_LIBRARIES} ${Qt5Widgets_LIBRARIES})
          set(Qt_COMPILE_DEFINE ${Qt5Core_COMPILE_DEFINITIONS} ${Qt5Gui_COMPILE_DEFINITIONS} ${Qt5Widgets_COMPILE_DEFINITIONS})
          set(CMAKE_AUTOMOC ON)
          set(CMAKE_INCLUDE_CURRENT_DIR ON)
          file(GLOB src_files "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
          file(GLOB src "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
          set(SOURCES ${src_files} ${src})
          add_executable(${PROJECT_NAME} ${SOURCES})
          target_include_directories(${PROJECT_NAME} PUBLIC ${Qt_INCLUDE} "${CMAKE_CURRENT_SOURCE_DIR}/src")
          target_link_libraries(${PROJECT_NAME} PUBLIC ${Qt_LIBS})
          target_compile_definitions(${PROJECT_NAME} PRIVATE ${Qt_COMPILE_DEFINE})
          set_property(TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
          
          Q Offline
          Q Offline
          qjqj
          wrote on last edited by
          #4

          @VRonin

          Hi VRonin,

          Sorry for the late reply. First of all thanks for the example that you have given.

          However, that way of doing is telling the machine to look for the package inside /usr/lib path and not looking at my project folder. As I have added the include & library folder needed for my project.

          Thanks once again and Merry Christmas to you!

          Q 1 Reply Last reply
          0
          • Q qjqj

            @VRonin

            Hi VRonin,

            Sorry for the late reply. First of all thanks for the example that you have given.

            However, that way of doing is telling the machine to look for the package inside /usr/lib path and not looking at my project folder. As I have added the include & library folder needed for my project.

            Thanks once again and Merry Christmas to you!

            Q Offline
            Q Offline
            qjqj
            wrote on last edited by
            #5

            @VRonin,

            Hi,

            I found the solution to set it to another path by setting the -DCMAKE_PREFIX_PATH during cmake

            Thanks!

            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 Extensions
            • Unsolved