CMake + vscode
-
wrote on 30 Jun 2022, 17:45 last edited by
Hi,
I'm trying to understand how to build a qt app on vscode.
I have a CMakeLists.txt which work but it is complex and I have the Qt Cmake example which build but no widow appears when i run it.The CMakeLists.txt which works:
cmake_minimum_required(VERSION 3.22) project(qt-cmake) set(CMAKE_CXX_STANDARD 14) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_PREFIX_PATH "C:/Tools/Qt/6.3.1/mingw_64") find_package(Qt6 COMPONENTS Widgets REQUIRED) add_executable(qt-cmake main.cpp) target_link_libraries(qt-cmake Qt::Widgets ) if (WIN32) set(DEBUG_SUFFIX) if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug") set(DEBUG_SUFFIX "d") endif () set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") if (NOT EXISTS "${QT_INSTALL_PATH}/bin") set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") if (NOT EXISTS "${QT_INSTALL_PATH}/bin") set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") endif () endif () if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/") add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/") endif () foreach (QT_LIB Core Gui Widgets) add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll" "$<TARGET_FILE_DIR:${PROJECT_NAME}>") endforeach (QT_LIB) endif ()
The CMakeLists.txt that i would like to make works:
cmake_minimum_required(VERSION 3.16) project(helloworld VERSION 1.0.0 LANGUAGES CXX) set(CMAKE_PREFIX_PATH "C:/Tools/Qt/6.3.1/mingw_64") set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 REQUIRED COMPONENTS Widgets) qt_standard_project_setup() add_executable(helloworld main.cpp ) target_link_libraries(helloworld PRIVATE Qt6::Widgets) set_target_properties(helloworld PROPERTIES WIN32_EXECUTABLE ON MACOSX_BUNDLE ON )
My setup:
Windows, mingw, Qt6 -
Hi and welcome to devnet,
Might be a silly question but do you show any widget in your application ?
If so, can you share your main.cpp file ?
Does the application start successfully ?
-
wrote on 30 Jun 2022, 17:59 last edited by
My main.cpp
#include <QApplication> #include <QPushButton> int main(int argc, char *argv[]) { QApplication a(argc, argv); QPushButton button("Hello world!", nullptr); button.resize(200, 100); button.show(); return QApplication::exec(); }
Both CMakeLists build the project but the second one do nothing when i run the build.
-
That looks perfectly fine.
How do you start your application ?
-
wrote on 30 Jun 2022, 18:10 last edited by
I use the .exe in my build folder.
-
And you have no error ? Just the application that is hanging ?
-
wrote on 30 Jun 2022, 18:39 last edited by mchinand
Your 'working' CMake project is copying the Qt dlls and plugin folders to where your .exe is created. You could copy those post-build steps to your other CMakeLists.txt file; the entire
if (WIN32)
block at the end of the CMakeLists.txt. -
wrote on 30 Jun 2022, 18:58 last edited by
It's not hanging it just runs without error.
Why i have to copy all that and can't use the CMake of the tutorial ? -
It's not hanging it just runs without error.
Why i have to copy all that and can't use the CMake of the tutorial ?wrote on 30 Jun 2022, 19:02 last edited byAlternatively, you can add the Qt bin folder to your PATH.
-
If you take Qt Creator, it will adapt the PATH environment variable so that it will contain the path for the Qt version you are using to build your application. If your current IDE does not provide that automation, then your application will need to have all its dependencies copied beside it. That is nothing Qt specific though, any dependency you have must be findable by your executable,
-
wrote on 30 Jun 2022, 19:19 last edited by
Thanks guys it works perfectly now.
6/11