I am trying to build a simple Qt Console Application outside of QtCreator on Windows and am facing some issues.
Environment: Qt 5.15, Windows
cmake_minimum_required(VERSION 3.29)
# Project name
project(QtConsoleApp)
# Set the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
# Find the Qt libraries
set(QT_DIR "C:/Qt/5.15.1/msvc2019_64")
set(QT5_DIR "C:/Qt/5.15.1/msvc2019_64/lib/cmake/Qt5")
set(CMAKE_PREFIX_PATH ${QT_DIR})
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui)
# Add executable
add_executable(QtConsoleApp src/main.cpp)
# Include directories
target_include_directories(QtConsoleApp PUBLIC include ${QT_DIR}/include/ ${QT_DIR}/include/QtCore/)
# Link libraries
target_link_libraries(QtConsoleApp Qt5::Core Qt5::Widgets Qt5::Gui)
When I tried the above example which seems like the most common way to do things I got this error
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/QtConsoleApp.dir/objects.a(main.cpp.obj): in function `main':
E://Simple Qt Console App/src/main.cpp:5:(.text+0x32): undefined reference to `__imp__ZN16QCoreApplicationC1ERiPPci'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E://Simple Qt Console App/src/main.cpp:6:(.text+0x66): undefined reference to `__imp__ZNK14QMessageLogger5debugEv'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E://Simple Qt Console App/src/main.cpp:6:(.text+0x89): undefined reference to `__imp__ZN6QDebugD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E://Simple Qt Console App/src/main.cpp:7:(.text+0x92): undefined reference to `__imp__ZN16QCoreApplication4execEv'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E://CMake/Simple Qt Console App/src/main.cpp:8:(.text+0xa5): undefined reference to `__imp__ZN16QCoreApplicationD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E://Simple Qt Console App/src/main.cpp:6:(.text+0xbc): undefined reference to `__imp__ZN6QDebugD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E://Simple Qt Console App/src/main.cpp:8:(.text+0xd1): undefined reference to `__imp__ZN16QCoreApplicationD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/QtConsoleApp.dir/objects.a(main.cpp.obj): in function `QString::fromUtf8(char const*, int)':
C:/Qt/5.15.1/msvc2019_64/include/QtCore/qstring.h:701:(.text$_ZN7QString8fromUtf8EPKci[_ZN7QString8fromUtf8EPKci]+0x45): undefined reference to `__imp__ZN7QString15fromUtf8_helperEPKci'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/QtConsoleApp.dir/objects.a(main.cpp.obj): in function `QDebug::maybeSpace()':
C:/Qt/5.15.1/msvc2019_64/include/QtCore/qdebug.h:129:(.text$_ZN6QDebug10maybeSpaceEv[_ZN6QDebug10maybeSpaceEv]+0x2d): undefined reference to `__imp__ZN11QTextStreamlsEc'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/QtConsoleApp.dir/objects.a(main.cpp.obj): in function `QDebug::operator<<(char const*)':
C:/Qt/5.15.1/msvc2019_64/include/QtCore/qdebug.h:159:(.text$_ZN6QDebuglsEPKc[_ZN6QDebuglsEPKc]+0x40): undefined reference to `__imp__ZN11QTextStreamlsERK7QString'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/QtConsoleApp.dir/objects.a(main.cpp.obj): in function `QTypedArrayData<unsigned short>::deallocate(QArrayData*)':
C:/Qt/5.15.1/msvc2019_64/include/QtCore/qarraydata.h:239:(.text$_ZN15QTypedArrayDataItE10deallocateEP10QArrayData[_ZN15QTypedArrayDataItE10deallocateEP10QArrayData]+0x21): undefined reference to `__imp__ZN10QArrayData10deallocateEPS_yy'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/QtConsoleApp.dir/build.make:121: QtConsoleApp.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:86: CMakeFiles/QtConsoleApp.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
Then I gave it another shot giving direct access to the DLL files in my Qt installation directory
cmake_minimum_required(VERSION 3.29)
# Project name
project(QtConsoleApp)
# Set the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
# Find the Qt libraries
set(QT_DIR "C:/Qt/5.15.1/msvc2019_64")
set(QT5_DIR "C:/Qt/5.15.1/msvc2019_64/lib/cmake/Qt5")
set(CMAKE_PREFIX_PATH ${QT_DIR})
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui)
# Add executable
add_executable(QtConsoleApp src/main.cpp)
# Include directories
target_include_directories(QtConsoleApp PUBLIC include ${QT_DIR}/include/ ${QT_DIR}/include/QtCore/)
# Link libraries
target_link_libraries(QtConsoleApp ${QT_DIR}/bin/Qt5Core ${QT_DIR}/bin/Qt5Widgets ${QT_DIR}/bin/Qt5Gui)
An got another error
make[2]: *** No rule to make target 'C:/Qt/5.15.1/msvc2019_64/bin/Qt5Core', needed by 'QtConsoleApp.exe'. Stop.
make[1]: *** [CMakeFiles/Makefile2:86: CMakeFiles/QtConsoleApp.dir/all] Error 2
make: *** [Makefile:91: all] Error 2