cmake_minimum_required(VERSION 3.5)

project(07_TCP LANGUAGES CXX)

# 将编译目录包含到Include目录
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
# 根据 Qt 版本的环境变量,设置使用的 Qt 版本
set(CMAKE_PREFIX_PATH $ENV{QTDIR5141_64})

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.

#if(ANDROID)
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
#    if (ANDROID_ABI STREQUAL "armeabi-v7a")
#        set(ANDROID_EXTRA_LIBS
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
#    endif()
#endif()

# 工程中会用到的Qt模块
find_package(Qt5 COMPONENTS Widgets Network REQUIRED)

if(ANDROID)
  add_library(07_TCP SHARED
    main.cpp
    serverwidget.cpp
    serverwidget.h
    serverwidget.ui
  )
else()
# 添加了新的类和ui后要把文件添加到这里
  add_executable(07_TCP
    main.cpp
    serverwidget.cpp
    serverwidget.h
    serverwidget.ui
    clientwidget.cpp
    clientwidget.h
    clientwidget.ui
  )
endif()

# 把用到的Qt库连接到项目上
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Widgets Qt5::Network)

参考:

  1. CMake中添加Qt模块的合理方法