Qt工程CMake编译

 

cmake_minimum_required(VERSION 3.5.1)

# https://blog.csdn.net/sinat_31964903/article/details/86615053
#指定Qt路径(按需设置)
#SET(CMAKE_PREFIX_PATH C:/Qt/Qt5.7.0/5.7/msvc2013/lib/cmake)
#list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

project(SmartInjection_CMake)

SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_EXTENSIONS OFF)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

SET(CMAKE_BUILD_TYPE "Release")


# console/ WinMain(Add_Executalble: + WIN32)
# set(QT_USE_QTMAIN TRUE) 

# 自动生成开关
set(CMAKE_AUTOMOC ON) 
#set(CMAKE_AUTOUIC ON) 
#set(CMAKE_AUTORCC ON) 	

if(CMAKE_VERSION VERSION_LESS "3.7.0")
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()

# Add Qt module
find_package(Qt5 COMPONENTS Core Gui Widgets Network Xml REQUIRED)

# Try to find OpenCV
SET(OpenCV_DIR E:/opencv_sourcecode/opencv_4.4.0/build/x64/vc15/lib) # Change to your own path
find_package(OpenCV REQUIRED)
if (OpenCV_FOUND)
    message(STATUS "OpenCV library status:")
    message(STATUS "    version: ${OpenCV_VERSION}")
    message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}" \n)
else ()
    message(FATAL_ERROR "Could not locate OpenCV" \n)
endif()

#Torch release
SET(Torch_DIR E:/Pytorch_ws/libtorch/share/cmake/Torch) # Change to your own path
find_package(Torch PATHS ${Torch_DIR} NO_DEFAULT REQUIRED)

if (Torch_FOUND)
    message(STATUS "Torch library found!")
    message(STATUS "    include path: ${TORCH_INCLUDE_DIRS}" \n)
else ()
    message(FATAL_ERROR "Could not locate Torch" \n)
endif()

include_directories(${PROJECT_SOURCE_DIR}/include)

#配置halcon,include、lib目录,具体的lib在target_link_libraries里(注意不加.lib后缀)
#系统环境变量$ENV,也可以换成自己的绝对路径
include_directories("$ENV{HALCONROOT}/include"
                    "$ENV{HALCONROOT}/include/halconcpp")
link_directories("$ENV{HALCONROOT}/lib/$ENV{HALCONARCH}")

# https://blog.csdn.net/xinjiang666/article/details/105722814

# Get *
set(src_dir .)
foreach(dir ${src_dir})
	string(TOUPPER "${dir}" dir_u)		
	
	
	file(GLOB ${dir_u}_UI_FILES ${dir}/*.ui)
	file(GLOB ${dir_u}_H_FILES ${dir}/*.h)
	file(GLOB ${dir_u}_CXX_FILES ${dir}/*.cpp)	
	
	list(APPEND UI_FILES ${${dir_u}_UI_FILES})
	list(APPEND HEADER_FILES ${${dir_u}_H_FILES})	
	list(APPEND SOURCE_FILES ${${dir_u}_CXX_FILES})
	
endforeach(dir ${src_dir})

# https://blog.csdn.net/bytxl/article/details/50635560
# message(STATUS "    SOURCE_FILES: ${SOURCE_FILES}" \n)
# list(REMOVE_ITEM SOURCE_FILES detect_cuda_compute_capabilities.cpp)
# message(STATUS "    SOURCE_FILES: ${SOURCE_FILES}" \n)

if(UI_FILES)
	qt5_wrap_ui( UIC_SRCS ${UI_FILES}) 
endif()

# *.qrc to qrc_*.cpp 资源文件 
file(GLOB RESOURCE_FILES *.qrc)
if(RESOURCE_FILES)
	qt5_add_resources(RCC_SRCS ${RESOURCE_FILES}) 
endif()

#file(GLOB SOURCE_FILES src/*.cpp)
# file(GLOB SOURCE_FILES src/main.cpp src/detector.cpp )

add_executable(${CMAKE_PROJECT_NAME}  ${SOURCE_FILES} 
				${HEADER_FILES}
				${UIC_SRCS}  # 添加此选项会生成 xxx_autogen 工程 
				#${UI_FILES} # 和${UIC_SRCS} 二选一
				${RCC_SRCS})

target_link_libraries (
        ${CMAKE_PROJECT_NAME}
        ${OpenCV_LIBS}
        ${TORCH_LIBRARIES}
		halcon halconcpp
		Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Network Qt5::Xml
)

MESSAGE(${CMAKE_BINARY_DIR}/../)
SET_TARGET_PROPERTIES(${EXECUTABLE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY  "${CMAKE_BINARY_DIR}/../")

# Filter 设置,VS中分类目录直观简洁				
source_group("Form Files" FILES ${UI_FILES})
source_group("Generated Files" FILES ${UIC_SRCS} ${RCC_SRCS} )
source_group("Resources" FILES ${RESOURCE_FILES})

参考:

qt 工程CMakeLists.txt_sinat_31964903的博客-CSDN博客

用 cmake 构建Qt工程(对比qmake进行学习)_Frestone的博客-CSDN博客

qt工程cmakelists编写注意事项_开心萝卜-CSDN博客

你可能感兴趣的:(Qt,c++,qt,cmake)