Qt6 CMAKE - Undefined Reference to 'WinMain'
-
Please post the full error message
wrt main.cpp compiler error - if you want to use a library (in the case QtWidgets) your target also must link against it which it currently doesn't.
-
@Christian-Ehrlicher Thank you for responding.
:-1: error: CMake process exited with exit code 1. :-1: error: CMake returned error code: 1 :-1: error: C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain' :-1: error: collect2.exe: error: ld returned 1 exit status C:\Users\UserName\Documents\Qt_Projects\cmake_with_tests\mainwindow.hpp:4: error: QMainWindow: No such file or directory In file included from C:/Users/UserName/Documents/Qt_Projects/cmake_with_tests/main.cpp:2: C:/Users/UserName/Documents/Qt_Projects/cmake_with_tests/mainwindow.hpp:4:10: fatal error: QMainWindow: No such file or directory 4 | #include <QMainWindow> | ^~~~~~~~~~~~~ :-1: error: ninja: build stopped: subcommand failed.
These are the full error messages.
Isn't my Target linking Qt Widgets here? ->
target_link_libraries(PROJECT_SOURCES PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
I am learning CMake, sorry for the errors I make. Thanks
-
@Kevin470 said in Qt6 CMAKE - Undefined Reference to 'WinMain':
Isn't my Target linking Qt Widgets here? -> target_link_libraries(PROJECT_SOURCES PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
No, you're on the wrong target - main.cpp is compiled for target PROJ_TARGET (strange target name btw)
-
@Christian-Ehrlicher Yeah the names are just for me to understand where the differences lie.
Do you mean like thistarget_link_libraries(PROJ_TARGET PRIVATE Qt${QT_VERSION_MAJOR}::Widgets PRIVATE PROJECT_SOURCES)
This removes the errorundefined Reference to 'WinMain'
. But the missing QMainWindow still remains. -
@Kevin470 said in Qt6 CMAKE - Undefined Reference to 'WinMain':
target_link_libraries(PROJ_TARGET PRIVATE Qt${QT_VERSION_MAJOR}::Widgets PROJECT_SOURCES)
-
@Christian-Ehrlicher This has the same error as well unfortunately. This removed the 'WinMain' issue. But missing QMainWindow still remains.
-
@Christian-Ehrlicher I am trying to take a similar approach to KDAB's example with the Qt Console Project. They did the same thing where they make an
add_library
called "QWAMLib" where they add all the header and source files except main.cpp.
Then they do atarget_link_libraries
to the "QWAMLib" withQt${QT_VERSION_MAJOR}::Core
. And then they make an executable with the main.cpp and target link libraries to the QWAMLib. The example is as follows.cmake_minimum_required(VERSION 3.9) project(UnitTests) set(CMAKE_CXX_STANDARD 11) set(CMAKE_INCLUDE_CURRENT_DIR ON) find_package(QT NAMES Qt5 Qt6 CONFIG REQUIRED COMPONENTS Core Test) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Test) set(CMAKE_AUTOMOC TRUE) set(CMAKE_AUTORCC TRUE) add_library(QWAMLib SHARED Gadgets.h NewWay.h NewWayUsingNameSpace.h OriginalWay.h) target_link_libraries(QWAMLib Qt${QT_VERSION_MAJOR}::Core) add_executable(QWAM main.cpp) target_link_libraries(QWAM QWAMLib) add_subdirectory(Tests)
This works. I thought what I did with the QMainWindow is very similar to this
-
No need to show me CMake examples. Please show your current CMakeLists.txt
-
@Christian-Ehrlicher Sorry about that. This is my Current CMakeLists.txt
cmake_minimum_required(VERSION 3.5) project(cmake_with_tests VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) option(ENABLE_TESTS "Enable Tests" ON) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Test) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Test) add_library(PROJECT_SOURCES SHARED # main.cpp mainwindow.cpp mainwindow.hpp mainwindow.ui adder.hpp adder.cpp ) #target_link_libraries(PROJECT_SOURCES PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) qt_add_executable(PROJ_TARGET MANUAL_FINALIZATION main.cpp ) else() add_executable(PROJ_TARGET main.cpp ) endif() target_link_libraries(PROJ_TARGET PRIVATE Qt${QT_VERSION_MAJOR}::Widgets PROJECT_SOURCES) set_target_properties(PROJ_TARGET PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE OUTPUT_NAME "CMake_Testing" ) install(TARGETS PROJ_TARGET BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) if(ENABLE_TESTS) add_subdirectory(tests) endif() if(QT_VERSION_MAJOR EQUAL 6) qt_finalize_executable(PROJ_TARGET) endif()
-
@Kevin470 said in Qt6 CMAKE - Undefined Reference to 'WinMain':
But the missing QMainWindow still remains.
No, it's now no longer in main.cpp but mainwindow.cpp because you now don't link your library against Qt6::Widgets because it's commented out
#target_link_libraries(PROJECT_SOURCES PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
-
@Christian-Ehrlicher Thank you so much.
I have a question on the linking. In the first CMakeLists.txt I showed,
I am adding a library called PROJECT_SOURCES which is then linked with the QT Widgets. And the PROJECT_SOURCES is linked to the executable with main.cpp. Won't that link the QT Widgets along with it as well? It might be a bad question, but it would help me understand how this works.Your suggestion that commenting it has brought the problem again has helped. This has cleared up the missing QMainWindow error. But the
Undefined reference to 'WinMain'
is thrown now. -
@Kevin470 said in Qt6 CMAKE - Undefined Reference to 'WinMain':
Won't that link the QT Widgets along with it as well? It might be a bad question, but it would help me understand how this works.
No because you told CMake that it's a PRIVATE library instead PUBLIC - see the cmake documentation. Linking it PRIVATE is wrong in my pov because your public headers include QtWidgets headers so it should be linked public.
-
@Christian-Ehrlicher I understand. This is my current CMakeLists.txt:
cmake_minimum_required(VERSION 3.5) project(cmake_with_tests VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) option(ENABLE_TESTS "Enable Tests" ON) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Test) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Test) add_library(PROJECT_SOURCES SHARED # main.cpp mainwindow.cpp mainwindow.hpp mainwindow.ui adder.hpp adder.cpp ) target_link_libraries(PROJECT_SOURCES PUBLIC Qt${QT_VERSION_MAJOR}::Widgets) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) qt_add_executable(PROJ_TARGET MANUAL_FINALIZATION main.cpp ) else() add_executable(PROJ_TARGET main.cpp ) endif() target_link_libraries(PROJ_TARGET PUBLIC Qt${QT_VERSION_MAJOR}::Widgets PROJECT_SOURCES) set_target_properties(PROJ_TARGET PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE OUTPUT_NAME "CMake_Testing" ) install(TARGETS PROJ_TARGET BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) if(ENABLE_TESTS) add_subdirectory(tests) endif() if(QT_VERSION_MAJOR EQUAL 6) qt_finalize_executable(PROJ_TARGET) endif()
Under errors, I have these following errors; The WinMain error is predominant.
:-1: error: CMake process exited with exit code 1. :-1: error: CMake returned error code: 1 :-1: error: C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain' :-1: error: collect2.exe: error: ld returned 1 exit status :-1: error: ninja: build stopped: subcommand failed.
And under General Messages, I checked what CMake prints as an error, it says:
CMake Error: Running 'C:/Qt/Tools/Ninja/ninja.exe' '-C' 'C:/Users/UserName/Documents/Qt_Projects/build-cmake_with_tests-Desktop_Qt_6_4_1_MinGW_64_bit-Debug' '-t' 'recompact' failed with: ninja: error: failed recompaction: Permission denied
-
@Kevin470 said in Qt6 CMAKE - Undefined Reference to 'WinMain':
ninja: error: failed recompaction: Permission denied
You run ninja as root before which you must not do.
Re-create your build dir or change the permissions / owner.Please also post some more lines - you only show the error message but not which command creates it.
-
@Christian-Ehrlicher
This was the full messages that it printed.Running C:\Qt\Tools\CMake_64\bin\cmake.exe -S C:/Users/UserName/Documents/Qt_Projects/cmake_with_tests -B C:/Users/UserName/Documents/Qt_Projects/build-cmake_with_tests-Desktop_Qt_6_4_1_MinGW_64_bit-Debug in C:\Users\UserName\Documents\Qt_Projects\build-cmake_with_tests-Desktop_Qt_6_4_1_MinGW_64_bit-Debug. -- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR) -- Configuring done -- Generating done CMake Error: Running 'C:/Qt/Tools/Ninja/ninja.exe' '-C' 'C:/Users/UserName/Documents/Qt_Projects/build-cmake_with_tests-Desktop_Qt_6_4_1_MinGW_64_bit-Debug' '-t' 'recompact' failed with: ninja: error: failed recompaction: Permission denied CMake Error: Running 'C:/Qt/Tools/Ninja/ninja.exe' '-C' 'C:/Users/UserName/Documents/Qt_Projects/build-cmake_with_tests-Desktop_Qt_6_4_1_MinGW_64_bit-Debug' '-t' 'restat' 'build.ninja' failed with: ninja: error: failed recompaction: Permission denied CMake Generate step failed. Build files cannot be regenerated correctly. CMake process exited with exit code 1. Elapsed time: 00:01.
I deleted the CMakeLists.txt.user file and reconfigured it from scratch. Now it builds fine and it works like expected.
Thank you so much for your time and your help.