Error including Qt5 Network in CMakeLists.txt?



  • I have very little experience with cmake, and I am struggling with including the modules I need to get the project building. In my .pro file I have the following inclusions:

    QT += core gui widgets printsupport network
    

    When I 'make' the result of running cmake, I get the following error.

    fatal error: QHostAddress: No such file or directory
    

    Here is my CMakeLists.txt file with some defining information removed. I am certain the error is occurring because I have not correctly included Qt Network, but I'm not sure exactly what I'm missing?

    cmake_minimum_required(VERSION 2.8.12)
    project(MyProject)
    add_definitions(-std=c++11)
    
    find_package(Qt5Widgets REQUIRED)
    find_package(Qt5PrintSupport REQUIRED)
    find_package(Qt5Network REQUIRED)
    
    include_directories(${Qt5Widgets_INCLUDE_DIRS})
    add_definitions(${Qt5Widgets_DEFINITIONS})
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
    
    SET(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    SET(MAIN main.cpp)
    SET(MYPROJECTSOURCE ...cpp)
    SET(MYPROJECTHEADERS ....h)
    
    SET(MYPROJECTUI ...ui)
    
    qt5_wrap_cpp(HEADERSMOC ${MYPROJECTHEADERS})
    qt5_wrap_ui (UIMOC  ${MYPROJECTUI})
    
    include_directories(${PROJECT_SOURCE_DIR})
    include_directories(${PROJECT_BINARY_DIR})
    
    add_library(MYPROJECTLIB SHARED
        ${MYPROJECTSOURCE}
        ${HEADERSMOC}
        ${UIMOC}
    )
    
    target_link_libraries(MYPROJECTLIB Qt5::Widgets)
    
    add_executable(MyProject ${MAIN})
    target_link_libraries(MyProject MYPROJECTLIB)
    target_link_libraries(MyProject Qt5::PrintSupport)
    target_link_libraries(MyProject Qt5::Network)
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Did you add the network module after having already configured your project ?

    Which version of cmake are you using ?

    By the way, you shouldn't need most these old Qt specific stuff you have currently. See the CMake manuel in Qt's documentation.



  • Hi, for making Qt project you have two option:
    1-one using qmake that it can find usually Qt parts library.
    2-cmake ,cmake can not find your files.cmake for your project:
    your post can be better with describing completely errors not part of it .
    for example for using from QtCore, cmake should find QtCoreConfig.cmake

    cmake will be need it.........
    so you shoud set CMAKE_PREFIX_PATH to cmake files directory......!
    in linux you should type :

    export CMAKE_PREFIX_PATH="QTPTH/..../lib/cmake"
    

    cmake error in Qt



  • You've added only Qt5Widgets to your target, so it cannot use Qt5Network



  • @SGaist said in Error including Qt5 Network in CMakeLists.txt?:

    Did you add the network module after having already configured your project ?

    I'm not sure I understand this? Can you elaborate?

    Which version of cmake are you using ?

    cmake version is 3.5.1

    Thanks!


  • Lifetime Qt Champion

    @Konstantin-Tokarev has a point, if you are using QHostAddress in your library target, you forgot to link against Qt5::network

    I was meaning that if you configured your project once and after that you added the dependency to the network module, you would likely need to do a full reconfigure of your project.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.