cmake 工程配置
# 指定版本和项目
cmake_minimum_required(VERSION 3.10)
set(TARGET_NAME labelDeviceView)
project(${TARGET_NAME} )
include(${CMAKE_CURRENT_LIST_DIR}/../../../../../../ossLib/ossLib/env.cmake)
set(CMAKE_PREFIX_PATH "D:/Qt6/6.5.2/msvc2019_64/")
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt6 COMPONENTS Widgets REQUIRED)
file(GLOB LD_INC "../../../include/*.*")
file(GLOB LD_SRC "./main/*.cpp" )
file(GLOB LD_MAIN_HEADER "./main/*.h")
file(GLOB LD_FORMS "./main/*.ui")
file(GLOB LD_RESOURCES
#main.qrc
)
#调用预编译器moc,需要使用 QT6_WRAP_CPP宏
QT6_WRAP_CPP(LD_MAIN_HEADER_MOC ${LD_MAIN_HEADER})
QT6_WRAP_UI(LD_FORMS_HEADERS ${LD_FORMS})
QT6_ADD_RESOURCES(LD_RESOURCES_RCC ${LD_RESOURCES})
include_directories(./)
include_directories(../../../include/)
include_directories(./main)
link_directories(${CMAKE_CURRENT_LIST_DIR}/../../../libs/${os}/${platform})
SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_LIST_DIR}/../../../bin/${os}/${platform})
add_executable(${TARGET_NAME}
${LD_SRC}
#${LD_MAIN_HEADER_MOC}
${LD_MAIN_HEADER}
${LD_FORMS_HEADERS}
${LD_FORMS}
${LD_RESOURCES_RCC}
${LD_RESOURCES}
)
target_link_libraries(${TARGET_NAME} ${Qt5Widgets_LIBRARIES} ${Qt5Charts_LIBRARIES} ${Qt5OpenGL_LIBRARIES})
# 配置命令行参数
set_property(TARGET ${TARGET_NAME} PROPERTY VS_DEBUGGER_COMMAND "./labelDeviceView.exe")
set_property(TARGET ${TARGET_NAME} PROPERTY VS_DEBUGGER_COMMAND_ARGUMENTS "./config/labelDriver.cfg")
set_property(TARGET ${TARGET_NAME} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "$(OutDir)/")
set_property(TARGET ${TARGET_NAME} PROPERTY VS_DEBUGGER_ENVIRONMENT "")
target_link_libraries(${TARGET_NAME} PRIVATE ${Qt6Widgets_LIBRARIES} Qt6::Widgets)
target_link_libraries(${TARGET_NAME} PRIVATE optimized ossLib debug ossLibd )
target_link_libraries(${TARGET_NAME} PRIVATE optimized labelUi debug labelUid)
add_dependencies(${TARGET_NAME} "logFile.drv")
set_target_properties (${TARGET_NAME} PROPERTIES
FOLDER "testMain"
)
INSTALL(TARGETS ${TARGET_NAME}
RUNTIME DESTINATION lib
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
metaObject的相关链接无法找到的问题,如下图:
2>mainwindow.obj : error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const * __cdecl MainWindow::metaObject(void)const " (?metaObject@MainWindow@@UEBAPEBUQMetaObject@@XZ)
2>mainwindow.obj : error LNK2001: 无法解析的外部符号 "public: virtual void * __cdecl MainWindow::qt_metacast(char const *)" (?qt_metacast@MainWindow@@UEAAPEAXPEBD@Z)
2>mainwindow.obj : error LNK2001: 无法解析的外部符号 "public: virtual int __cdecl MainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MainWindow@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
2>mainwindow.obj : error LNK2019: 无法解析的外部符号 "public: void __cdecl MainWindow::initDrivers(void)" (?initDrivers@MainWindow@@QEAAXXZ),函数 "public: __cdecl MainWindow::MainWindow(char const *,class QWidget *)" (??0MainWindow@@QEAA@PEBDPEAVQWidget@@@Z) 中引用了该符号
2>mainwindow.obj : error LNK2001: 无法解析的外部符号 "public: static struct QMetaObject const MainWindow::staticMetaObject" (?staticMetaObject@MainWindow@@2UQMetaObject@@B)
2>E:\gitee\Development\C++\labelDriver\labelDriver4.0\labelDriver\bin\windows\x64\Debug\labelDeviceView.exe : fatal error LNK1120: 5 个无法解析的外部命令
这个问题的原因是因为没有生成对应的Moc文件,或者moc文件为空。没有生产Moc文件是因为对应的开关没有开,配置:
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
并且在对应的类中天界Q_OBJECT声明。
Moc文件为空则需要考虑加入QT6_WRAP_CPP(LD_MAIN_HEADER_MOC ${LD_MAIN_HEADER})的文件是否正确,要特别注意${LD_MAIN_HEADER}只能加入头文件,不能包含cpp文件,否则就可能导致moc文件为空。