Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

CMAKE with QT library



  • 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}
    )
    


  • 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)
    


  • This post is deleted!


  • @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!



  • @VRonin,

    Hi,

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

    Thanks!


Log in to reply