一下全部内容全都是大佬lht的经验,我只是记录一下给大家。
问题:Qt5Config.cmake找不到
解决办法:
set(CMAKE_PREFIX_PATH "E:/Qt/Qt5.12.11/5.12.11/mingw73_64/lib/cmake/Qt5")
找到Qt5Config.cmake路径,添加上面这句
或者
-D CMAKE_PREFIX_PATH = "******"
在红线区域加上上面那句话,空格可以忽略
问题:ui的头文件找不到
解决办法:
设生成的目标为 Test ,在 CMakeLists.txt 文件的最后一行添加:
target_include_directories(Test PRIVATE "${CMAKE_BINARY_DIR}/Test_autogen/include")#Test_autogen文件夹名字注意
问题:首次运行QT0xC0000135错误
解决办法:
编译成功后无法运行缺依赖,学官网加环境变量是最好的
编译完成后,在qt里找到windeployqt.exe 运行
windeployqt.exe xxxx.exe
#XXXX.exe就是你编译后的exe
它会把依赖文件复制到编译文件夹内,然后再运行。上述步骤只需要进行一次,除非你引用了新的库
问题:编译到链接库阶段报错moc_mainwindow.cpp:99: undefined reference to ‘__imp__
CMakeFiles\QTPlayer.dir/objects.a(mainwindow.cpp.obj): In function `MainWindow::MainWindow(QWidget*)':
E:/GitProject/QTPlayer/mainwindow.cpp:7: undefined reference to `__imp__ZN10QWebSocketC1ERK7QStringN18QWebSocketProtocol7VersionEP7QObject'
E:/GitProject/QTPlayer/mainwindow.cpp:7: undefined reference to `__imp__ZN10QWebSocketD1Ev'
CMakeFiles\QTPlayer.dir/objects.a(mainwindow.cpp.obj): In function `MainWindow::connectServer()':
E:/GitProject/QTPlayer/mainwindow.cpp:16: undefined reference to `__imp__ZN10QWebSocket9connectedEv'
E:/GitProject/QTPlayer/mainwindow.cpp:18: undefined reference to `__imp__ZN10QWebSocket4openERK4QUrl'
CMakeFiles\QTPlayer.dir/objects.a(mainwindow.cpp.obj): In function `MainWindow::onConnected()':
E:/GitProject/QTPlayer/mainwindow.cpp:23: undefined reference to `__imp__ZN10QWebSocket15sendTextMessageERK7QString'
CMakeFiles\QTPlayer.dir/objects.a(mainwindow.cpp.obj): In function `MainWindow::~MainWindow()':
E:/GitProject/QTPlayer/mainwindow.cpp:36: undefined reference to `__imp__ZN10QWebSocketD1Ev'
解决办法:
三类问题,
第一是在头文件声明的函数没写定义(网上说的)
(我不是这个问题,而且我觉得这不会导致编译直接过不了)
第二个是编译链同时装了msvc和mingw的库,混合编译了。解决方案见最后
https://stackoverflow.com/questions/39063110/lots-of-undefined-reference-in-qt-creator
第三种问题和用了websocket等插件类(它官网是这么描述的)的类功能,需要进行如下修改
find_package(QT NAMES Qt6 Qt5 COMPONENTS WebSockets Widgets REQUIRED)
target_link_libraries(QTPlayer PRIVATE Qt5::Widgets Qt5::WebSockets)
这类似于qt的pro文件里的
QT+=WebSockets
新发现一种情况,在qtcreator可以编译clion却不行,原因是多套qt,没有用正确的qt
解决方案是
-DCMAKE_BUILD_TYPE:STRING=Debug
-DCMAKE_C_COMPILER:STRING=D:/Qt/Qt5.12.11/Tools/mingw730_64/bin/gcc.exe
-DCMAKE_GNUtoMS:BOOL=OFF
-DCMAKE_INSTALL_PREFIX:PATH=C:/Program
Files
(x86)/XiaoZhi
-DCMAKE_PREFIX_PATH:STRING=D:/Qt/Qt5.12.11/5.12.11/mingw73_64
-DCMAKE_PROJECT_INCLUDE_BEFORE:PATH=D:/Qt/Qt5.12.11/Tools/QtCreator/share/qtcreator/package-manager/auto-setup.cmake
-DQT_DIR:PATH=D:/Qt/Qt5.12.11/5.12.11/mingw73_64/lib/cmake/Qt5
-DQT_QMAKE_EXECUTABLE:STRING=D:/Qt/Qt5.12.11/5.12.11/mingw73_64/bin/qmake.exe
-DQt5Core_DIR:PATH=D:/Qt/Qt5.12.11/5.12.11/mingw73_64/lib/cmake/Qt5Core
-DQt5Gui_DIR:PATH=D:/Qt/Qt5.12.11/5.12.11/mingw73_64/lib/cmake/Qt5Gui
-DQt5Widgets_DIR:PATH=D:/Qt/Qt5.12.11/5.12.11/mingw73_64/lib/cmake/Qt5Widgets
-DQt5_DIR:PATH=D:/Qt/Qt5.12.11/5.12.11/mingw73_64/lib/cmake/Qt5
复制于qtcreator
问题:Error copying file "******" to "*******"
解决办法:
删除CMakelist文件最后面的
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${QT_INSTALL_PATH}/bin/Qt5${QT_LIB}${DEBUG_SUFFIX}.dll" "$<TARGET_FILE_DIR:${PROJECT_NAME}>")