Adding wxWidgets to a project using cmake in Windows - c++

I am trying to add wxWidgets to my project using cmake.
in cmake I have:
#wxWidgets
set(wxWidgets_CONFIGURATION mswu)
find_package(wxWidgets COMPONENTS core base REQUIRED)
include(${wxWidgets_USE_FILE})
and for my project in cmake I have:
target_link_libraries(MyProject
${OpenCV_LIBS}
${wxWidgets_LIBRARIES}
${Boost_LIBRARIES})
and then run cmake using:
cmake -G "Visual Studio 11 Win64" ..
but when I compile the generated project in visual studio I am getting link error:
unresolved external symbol "public: __cdecl wxSplashScreen::wxSplashScreen(class wxBitmap const &,long,int,class wxWindow *,int,class wxPoint const &,class wxSize const &,long)" (??0wxSplashScreen##QEAA#AEBVwxBitmap##JHPEAVwxWindow##HAEBVwxPoint##AEBVwxSize##J#Z) referenced in function "public: __cdecl MainWindow::MainWindow(void)" (??0MainWindow##QEAA#XZ)
unresolved external symbol "public: virtual __cdecl wxSplashScreen::~wxSplashScreen(void)" (??1wxSplashScreen##UEAA#XZ) referenced in function "public: __cdecl MainWindow::MainWindow(void)" (??0MainWindow##QEAA#XZ)
unresolved external symbol main referenced in function __tmainCRTStartup
What is the problem and how can I fix it?

wxSplashScreen is part of the wxAdvanced library, so you need to tell cmake to link to that as well as core and base:
find_package(wxWidgets COMPONENTS core base adv REQUIRED)

Related

wxWidgets not linking using Cmake from VSCode (MSVC)

Problem
I am getting linker errors when building my project. The project has no trouble finding wxWidgets headers. I have looked at the wxWidgets minimal example and recreated it in order to verify that my build from source worked properly. I used cmake in VSCode to do the build using the cmake extension.
The wxWidgets cmake generated fine and the build of wxwidgets appears to have gone fine. I am using the Visual Studio 2022 amd64 toolset to do the compile. (I installed VS2022 Community Edition for the compiler only).
It looks like it is building DLL for the libraries. I would prefer static libraries but for an initial run I didnt think it mattered. I link my application to wx::net wx::core wx::base as recommended. I thought that this would take care of linking when I go to build.
When I go to build my overall application utilizing the wxWidgets library, I get these linker errors.
What am I doing wrong here? Im obviously not configuring something correctly for it to find the library, but I am kind of stumped here.
Linker Errors
[build] ProjectName.obj : error LNK2001: unresolved external symbol "protected: static class wxAppConsole * (__cdecl* wxAppConsoleBase::ms_appInitFn)(void)" -snipped-
[build] ProjectName.obj : error LNK2001: unresolved external symbol "protected: static class wxAppConsole * wxAppConsoleBase::ms_appInstance" -snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "void (__cdecl* wxTheAssertHandler)(class wxString const &,int,class wxString const &,class wxString const &,class wxString const &)"-snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "bool wxTrapInAssert" (?wxTrapInAssert##3_NA) -snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "class wxMBConv * wxConvLibcPtr" (?wxConvLibcPtr##3PEAVwxMBConv##EA) -snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "public: static unsigned __int64 const wxString::npos" (?npos#wxString##2_KB)-snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "class wxSize const wxDefaultSize" (?wxDefaultSize##3VwxSize##B)-snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "class wxPoint const wxDefaultPosition" (?wxDefaultPosition##3VwxPoint##B)-snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "char const * const wxFrameNameStr" (?wxFrameNameStr##3QBDB)-snipped-
[build] C:\path\to\executable\ProjectName.exe : fatal error LNK1120: 9 unresolved externals -snipped-
[build] Build finished with exit code 1
Project Layout
Pretty basic project layout. I am building a GUI application and I am logically separating out the ui source code.
This is basically a recreation of the minimal wxWidgets example where you have an entry point (Projectname.hpp and ProjectName.cpp provide this) and a main window/frame (MainWindow.hpp and MainWindow.cpp provide this). The ProjectName application will do the wxIMPLEMENT_APP(ProjectName) and will hold a pointer to a MainWindow. The MainWindow is shown using the OnInit Function.
ProjectName/
build/
external/
wxWidgets-3.1.6/
-CMakeLists.txt
source/
ui/
-MainWindow.hpp
-MainWindow.cpp
-ProjectName.hpp
-ProjectName.cpp
-CMakeLists.txt
I have compiled the wxWidgets library using their cmake instructions for using a subdirectory in cmake so I could add it to my project. Here are the contents of the CMakeLists.txt's
ProjectName CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
set(MAJOR 0)
set(MINOR 0)
set(PATCH 1)
#Setup Project
project(ProjectNameProject VERSION ${MAJOR}.${MINOR}.${PATCH}
DESCRIPTION "ProjectName Descrip"
LANGUAGES CXX)
add_executable(ProjectName WIN32 ${PROJECT_SOURCE_DIR}/source/ProjectName.cpp
${PROJECT_SOURCE_DIR}/source/ui/MainWindow.cpp)
target_include_directories(ProjectName PRIVATE ${PROJECT_SOURCE_DIR}/source)
target_include_directories(ProjectName PRIVATE ${PROJECT_SOURCE_DIR}/source/ui)
target_link_libraries(ProjectName PRIVATE wx::net wx::core wx::base)
add_subdirectory(${CMAKE_SOURCE_DIR}/external)
ProjectName/externals CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
add_subdirectory(${CMAKE_SOURCE_DIR}/external/wxWidgets-3.1.6)
Update
I changed the Cmake option for building a shared library (DLL) and set it to OFF. When this is done, it builds a static library (LIB). This seemed to fix my linking issues.
Per Igor's suggestion, I took another look at the minimal sample CMakeLists.txt and noticed they used the following line for linking
target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})
Perhaps my line (below) is the way to link statically and does not work with DLLs?
target_link_libraries(ProjectName PRIVATE wx::net wx::core wx::base)
If someone could help me understand why having it build statically fixed it, I would 100% accept that answer.
I changed the Cmake option for building a shared library (DLL) and set it to OFF. When this is done, it builds a static library (LIB). This seemed to fix my linking issues.
Unfortunately I do no see anyone able to chime in to help understand why this is the solution, but I am going to select this as an answer for others coming to this in the future.
If I see any updates that answer the question of "why" I will accept their answer.

Building GStreamer with CMake causes SDP & WebRTC unresolved external symbol errors

I'm building a C++ GStreamer project with CMake which depends on GStreamer, GLIB, Libsoup and json-glib. I'm new to CMake and having trouble setting up my project. I've managed to include many of the dependencies but some seem to remain unresolved even though they are part of GStreamer. All GStreamer methods and types are resolved with the exception of SDP and WebRTC. They are, to my understanding, part of GStreamer and are also located inside of the directory which GMake correctly "finds".
These are the errors that are occurring when trying to build the project.
[build] error LNK2019: unresolved external symbol __imp_gst_sdp_message_new referenced in function "void __cdecl soup_websocket_message_cb(struct _SoupWebsocketConnection *,enum SoupWebsocketDataType,struct _GBytes *,void *)" (?soup_websocket_message_cb##YAXPEAU_SoupWebsocketConnection##W4SoupWebsocketDataType##PEAU_GBytes##PEAX#Z)
[build] error LNK2019: unresolved external symbol __imp_gst_sdp_message_parse_buffer referenced in function "void __cdecl soup_websocket_message_cb(struct _SoupWebsocketConnection *,enum SoupWebsocketDataType,struct _GBytes *,void *)" (?soup_websocket_message_cb##YAXPEAU_SoupWebsocketConnection##W4SoupWebsocketDataType##PEAU_GBytes##PEAX#Z)
[build] error LNK2019: unresolved external symbol __imp_gst_sdp_message_as_text referenced in function "void __cdecl on_offer_created_cb(struct _GstPromise *,void *)" (?on_offer_created_cb##YAXPEAU_GstPromise##PEAX#Z)
[build] error LNK2019: unresolved external symbol __imp_gst_webrtc_session_description_get_type referenced in function "void __cdecl on_offer_created_cb(struct _GstPromise *,void *)" (?on_offer_created_cb##YAXPEAU_GstPromise##PEAX#Z)
[build] error LNK2019: unresolved external symbol __imp_gst_webrtc_session_description_new referenced in function "void __cdecl soup_websocket_message_cb(struct _SoupWebsocketConnection *,enum SoupWebsocketDataType,struct _GBytes *,void *)" (?soup_websocket_message_cb##YAXPEAU_SoupWebsocketConnection##W4SoupWebsocketDataType##PEAU_GBytes##PEAX#Z)
[build] error LNK2019: unresolved external symbol __imp_gst_webrtc_session_description_free referenced in function "void __cdecl on_offer_created_cb(struct _GstPromise *,void *)" (?on_offer_created_cb##YAXPEAU_GstPromise##PEAX#Z)
This is my CMakeLists.txt
# CMakeList.txt : CMake project for stream-project, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
project (stream-project LANGUAGES CXX)
# Packages
find_package(PkgConfig REQUIRED)
# Add source to this project's executable.
add_executable (${PROJECT_NAME} "main.cpp" "main.h")
# Search all modules that we so desire to use and "include_directories"
pkg_search_module(GST REQUIRED gstreamer-1.0 gstreamer-sdp-1.0 gstreamer-video-1.0 gstreamer-app-1.0 gstreamer-webrtc-1.0)
pkg_search_module(GLIB REQUIRED glib-2.0)
pkg_search_module(LIBSOUP REQUIRED libsoup-2.4)
pkg_search_module(JSONGLIB REQUIRED json-glib-1.0)
include_directories(
${GST_INCLUDE_DIRS}
${GLIB_INCLUDE_DIRS}
${LIBSOUP_INCLUDE_DIRS}
${JSONGLIB_INCLUDE_DIRS}
)
# Link target directories and libraries
target_link_directories(${PROJECT_NAME} PRIVATE
${GST_LIBRARY_DIRS}
${GLIB_LIBRARY_DIRS}
${LIBSOUP_LIBRARY_DIRS}
${JSONGLIB_LIBRARY_DIRS}
)
target_link_libraries(${PROJECT_NAME} PRIVATE
${GST_LIBRARIES}
${GLIB_LIBRARIES}
${LIBSOUP_LIBRARIES}
${JSONGLIB_LIBRARIES}
)
message(STATUS ${GST_INCLUDE_DIRS})
I've managed to solve it by using a premade find script I found online.
https://chromium.googlesource.com/external/Webkit/+/master/Source/cmake/FindGStreamer.cmake
It creates all necessary defines which I then include and link.
These are the defaults as specified in the FindGStreamer.cmake file
FIND_GSTREAMER_COMPONENT(GSTREAMER_APP gstreamer-app-1.0 gst/app/gstappsink.h gstapp-1.0)
FIND_GSTREAMER_COMPONENT(GSTREAMER_AUDIO gstreamer-audio-1.0 gst/audio/audio.h gstaudio-1.0)
FIND_GSTREAMER_COMPONENT(GSTREAMER_FFT gstreamer-fft-1.0 gst/fft/gstfft.h gstfft-1.0)
FIND_GSTREAMER_COMPONENT(GSTREAMER_PBUTILS gstreamer-pbutils-1.0 gst/pbutils/pbutils.h gstpbutils-1.0)
FIND_GSTREAMER_COMPONENT(GSTREAMER_VIDEO gstreamer-video-1.0 gst/video/video.h gstvideo-1.0)
I extended those above with:
FIND_GSTREAMER_COMPONENT(GSTREAMER_SDP gstreamer-sdp-1.0 gst/sdp/sdp.h gstsdp-1.0)
FIND_GSTREAMER_COMPONENT(GSTREAMER_WEBRTC gstreamer-webrtc-1.0 gst/webrtc/webrtc.h gstwebrtc-1.0)

Error linking boost with Visual Studio and vcpkg

I am trying to accomplish a simple task in order to check how easy is to develop "Linux apps" in Windows.
I am using vcpkg from Microsoft in order to setup headers and libs and crow , an c++ http server that uses boost as a driver project
The source is simple
#include "crow.hpp"
int main()
{
crow::SimpleApp app;
CROW_ROUTE(app, "/")
([]() {
return "Hello world!";
});
app.port(18080).run();
}
I have downloaded boost via vcpkg and used Visual Studio to code the project
cmake:
cmake_minimum_required (VERSION 3.8)
find_package(boost COMPONENTS filesystem system thread date_time REQUIRED)
message("this is ${Boost_INCLUDE_DIRS}")
include_directories ("C:\\github.com\\crow\\include")
include_directories(SYSTEM PUBLIC ${Boost_INCLUDE_DIRS})
add_executable (engine "promo-engine-windows.cpp" "promo-engine-windows.h")
target_link_libraries(engine ${Boost_LIBRARIES})
CMakeSettings.json
{
"configurations": [
{
"name": "x86-Debug",
"generator": "Visual Studio 15 2017",
"configurationType": "Debug",
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-m -v:minimal",
"variables": [
{
"name": "CMAKE_TOOLCHAIN_FILE",
"value": "C:\\github.com\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"
}
]
}
]
}
Linking gives errors:
error LNK2019: unresolved external symbol "__declspec(dllimport) class boost::system::error_category const & __cdecl boost::system::system_category(void)" (__imp_?system_category#system#boost##YAABVerror_category#12#XZ) referenced in function "public: __thiscall boost::system::error_code::error_code(void)" (??0error_code#system#boost##QAE#XZ)
error LNK2019: unresolved external symbol "__declspec(dllimport) class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (__imp_?generic_category#system#boost##YAABVerror_category#12#XZ) referenced in function "public: virtual bool __thiscall boost::system::error_category::std_category::equivalent(class std::error_code const &,int)const " (?equivalent#std_category#error_category#system#boost##UBE_NABVerror_code#std##H#Z)
error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall boost::gregorian::greg_month::greg_month(unsigned short)" (__imp_??0greg_month#gregorian#boost##QAE#G#Z) referenced in function "public: __thiscall boost::gregorian::date::date(enum boost::date_time::special_values)" (??0date#gregorian#boost##QAE#W4special_values#date_time#2##Z)
error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall boost::gregorian::greg_month::operator unsigned short(void)const " (__imp_??Bgreg_month#gregorian#boost##QBEGXZ) referenced in function "public: static unsigned int __cdecl boost::date_time::gregorian_calendar_base<struct boost::date_time::year_month_day_base<class boost::gregorian::greg_year,class boost::gregorian::greg_month,class boost::gregorian::greg_day>,unsigned int>::day_number(struct boost::date_time::year_month_day_base<class boost::gregorian::greg_year,class boost::gregorian::greg_month,class boost::gregorian::greg_day> const &)" (?day_number#?$gregorian_calendar_base#U?$year_month_day_base#Vgreg_year#gregorian#boost##Vgreg_month#23#Vgreg_day#23##date_time#boost##I#date_time#boost##SAIABU?$year_month_day_base#Vgreg_year#gregorian#boost##Vgreg_month#23#Vgreg_day#23##23##Z)
fatal error LNK1120: 4 unresolved externals
Build failed
Does it ring a bell to anyone? I am struggling hours with this.
You should change your cmake file to exactly match https://github.com/Microsoft/vcpkg/blob/master/ports/boost/usage.
e.g. in find_package the package name should be Boost not boost, adding REQUIRED would make it fail if boost isn't found, cmake is possibly not finding boost and silently ignoring it.

Unresolved Externals using Cmake and Visual Studio 2013

I've been having problems with cmake and Visual Studio 2013 with SFML. When I try and compile my program, I'm getting unresolved externals for everything that uses SFML. I've had problems with this on multiple machines, and also with some other libraries in some case.
I'm using the following command to generate my Visual Studio Project (inside a VisualStudioProject folder):
Z:/Coding/cmake/cmake-3.1.1-win32-x86/bin/cmake.exe -G "Visual Studio 12" ..
And this is my CMakeLists.txt (it's actually generated from a python script I wrote, but I doubt that's important). I can confirm that all the libraries are being found correctly.
cmake_minimum_required (VERSION 2.6)
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/lib )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/lib )
project(Game)
include_directories( Z:/Coding/Libraries/SFML/SFML-2.2/include Z:/Coding/Game/src/win32 Z:/Coding/Game/src )
find_library( SFML-GRAPHICS NAMES sfml-graphics PATHS Z:/Coding/Libraries/SFML/SFML-2.2/lib NO_DEFAULT_PATH )
find_library( SFML-WINDOW NAMES sfml-window PATHS Z:/Coding/Libraries/SFML/SFML-2.2/lib NO_DEFAULT_PATH )
find_library( SFML-SYSTEM NAMES sfml-system PATHS Z:/Coding/Libraries/SFML/SFML-2.2/lib NO_DEFAULT_PATH )
add_executable( win32 "Z:/Coding/Game/apps/win32.cpp" )
target_link_libraries( win32 ${SFML-GRAPHICS} ${SFML-WINDOW} ${SFML-SYSTEM} )
This is my simple SFML test program:
#include <string>
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
int main(int argc, char *argv[])
{
sf::RenderWindow window;
window.create(sf::VideoMode(1280, 1024), "Test");
while (true)
{
sf::Event ev;
while (window.pollEvent(ev))
{
if (ev.type = sf::Event::EventType::Closed)
{
window.close();
exit(1);
}
}
}
std::string hello = "Hello";
cout << hello << endl;
}
And I'm getting unresolved externals for the following:
Error 1 error LNK2019: unresolved external symbol
"__declspec(dllimport) public: __thiscall sf::String::String(char
const *,class std::locale const &)"
(__imp_??0String#sf##QAE#PBDABVlocale#std###Z) referenced in function
_main Z:\Coding\Game\VisualStudioProject\win32.obj win32
Error 2 error LNK2019: unresolved external symbol
"__declspec(dllimport) public: __thiscall sf::String::~String(void)"
(__imp_??1String#sf##QAE#XZ) referenced in function
_main Z:\Coding\Game\VisualStudioProject\win32.obj win32
Error 3 error LNK2019: unresolved external symbol
"__declspec(dllimport) public: __thiscall
sf::VideoMode::VideoMode(unsigned int,unsigned int,unsigned int)"
(__imp_??0VideoMode#sf##QAE#III#Z) referenced in function
_main Z:\Coding\Game\VisualStudioProject\win32.obj win32
Error 4 error LNK2019: unresolved external symbol
"__declspec(dllimport) public: void __thiscall
sf::Window::create(class sf::VideoMode,class sf::String const
&,unsigned int,struct sf::ContextSettings const &)"
(__imp_?create#Window#sf##QAEXVVideoMode#2#ABVString#2#IABUContextSettings#2##Z)
referenced in function
_main Z:\Coding\Game\VisualStudioProject\win32.obj win32
Error 5 error LNK2019: unresolved external symbol
"__declspec(dllimport) public: void __thiscall
sf::Window::close(void)" (__imp_?close#Window#sf##QAEXXZ) referenced
in function _main Z:\Coding\Game\VisualStudioProject\win32.obj win32
Error 6 error LNK2019: unresolved external symbol
"__declspec(dllimport) public: bool __thiscall
sf::Window::pollEvent(class sf::Event &)"
(__imp_?pollEvent#Window#sf##QAE_NAAVEvent#2##Z) referenced in
function _main Z:\Coding\Game\VisualStudioProject\win32.obj win32
Error 7 error LNK2019: unresolved external symbol
"__declspec(dllimport) public: __thiscall
sf::RenderWindow::RenderWindow(void)"
(__imp_??0RenderWindow#sf##QAE#XZ) referenced in function
_main Z:\Coding\Game\VisualStudioProject\win32.obj win32
Error 8 error LNK2019: unresolved external symbol
"__declspec(dllimport) public: virtual __thiscall
sf::RenderWindow::~RenderWindow(void)"
(__imp_??1RenderWindow#sf##UAE#XZ) referenced in function
_main Z:\Coding\Game\VisualStudioProject\win32.obj win32
Error 9 error LNK1120: 8 unresolved
externals Z:\Coding\Game\bin\Debug\win32.exe win32
I've used this same style of script before with SFML in the past on Windows with an earlier VS successfully (at least I recall I have) so I think it's a new thing with VS 2013, but I'm utterly stumped.
Thanks in advance.
Note: This is a simplistic example. This style of CMakeLists.txt is generated by script because the main code-base it's used on is very large (originally Linux based, hence why '/'s are used everywhere).
This certainly looks like you've not actually linked any of the three SFML libs successfully.
If the find_library calls in the CMakeLists.txt had failed to find the libraries, then CMake would output a fatal error which I'm sure you'd have mentioned.
So my best guess is that you're trying to link the 64-bit version of the SFML libs, while your CMake command specifies a 32-bit build.
To create a 64-bit build, just run:
cmake -G "Visual Studio 12 Win64" ..
You'll need to empty your build folder to change the generator from "Visual Studio 12" to "Visual Studio 12 Win64".
See the docs for further info about the VS generator.
Stab in the dark:
Try running
Z:/Coding/cmake/cmake-3.1.1-win32-x86/bin/cmake.exe -G "Visual Studio 12 2013 Win64"
This will force it to do all the linking in a 64-bit environment. I am presuming you're using a 64-bit computer. I'm further presuming you have a 64-bit version of SFML. (It's the future, I think these are safe assumptions).
CMake will try and pick a "sensible" default as per http://www.cmake.org/cmake/help/v3.0/generator/Visual%20Studio%2012%202013.html but sometimes you want to force it to pick the right option.

Linking Error using Cmake file to build Qt project in Visual Studio

I am trying to build a qt project using cmake. As a startinng point, I started off by just writing a simple code in qt creator, which contains main.cpp, mainwindow.cpp & mainwindow.h. Below are my CMakeLists.txt configuration.
cmake_minimum_required(VERSION 2.8.11)
project(testproject)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Find the QtWidgets library
find_package(Qt5Widgets)
find_package(Qt5Core)
get_target_property(QtCore_location Qt5::Core LOCATION)
# Tell CMake to create the executable
add_executable(testproject WIN32 main.cpp mainwindow.cpp)
# Use the Widgets module from Qt 5.
target_link_libraries(testproject Qt5::Widgets)
Cmake did successfully generate the VS Project files. However, when I try to build them in visual studio, it gives me linking errors. Some of the errors are listed below. I could not post the whole error list as it is too long.
>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl QWidget::show(void)" (__imp_?show#QWidget##QEAAXXZ) referenced in function main
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QApplication::QApplication(int &,char * *,int)" (__imp_??0QApplication##QEAA#AEAHPEAPEADH#Z) referenced in function main
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl QApplication::~QApplication(void)" (__imp_??1QApplication##UEAA#XZ) referenced in function main
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static int __cdecl QApplication::exec(void)" (__imp_?exec#QApplication##SAHXZ) referenced in function main
2>mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QMetaObject::Connection::~Connection(void)" (__imp_??1Connection#QMetaObject##QEAA#XZ) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
2>mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QString::~QString(void)" (__imp_??1QString##QEAA#XZ) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
2>testproject_automoc.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual int __cdecl QMainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (__imp_?qt_metacall#QMainWindow##UEAAHW4Call#QMetaObject##HPEAPEAX#Z) referenced in function "public: virtual int __cdecl MainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#MainWindow##UEAAHW4Call#QMetaObject##HPEAPEAX#Z)
2>testproject_automoc.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static struct QMetaObject const QMainWindow::staticMetaObject" (__imp_?staticMetaObject#QMainWindow##2UQMetaObject##B) referenced in function "void __cdecl `dynamic initializer for 'public: static struct QMetaObject const MainWindow::staticMetaObject''(void)" (??__E?staticMetaObject#MainWindow##2UQMetaObject##B##YAXXZ)
2>MSVCRT.lib(crtexew.obj) : error LNK2019: unresolved external symbol WinMain referenced in function __tmainCRTStartup
I am using Qt5.3.1, cmake 3.0.2 and visual studio 2012.
In order to fix this issue you have to remove the WIN32 statement from
add_executable(testproject WIN32 main.cpp mainwindow.cpp).
Possible causes:
Check the source file names in the add_executable command inside your CMakeLists.txt for spelling errors. File names in the source list that do not exist will produce the linker error error LNK2019: unresolved external symbol main referenced.
What does your entry point function look like?
WinMain requires add_executable(testproject WIN32 main.cpp mainwindow.cpp)
main requires add_executable(testproject main.cpp mainwindow.cpp)
Note: I suggest using int main(int argc, char *argv[]), as it will be portable to linux, while using WinMain won't.
I just ran into this error, I was using the x64-Debug configuration in Visual Studio, but the variable CMAKE_PREFIX_PATH in CMake was the path of x86 Qt, so the linker tried to link the x86 Qt libraries to the x64 program and the error occurred.
The solution is correcting the Visual Studio configuration and the Qt path to the same architecture.
You have to link against QtCore, you only link against Qt5::Widgets. Add Qt5::Core to your link libraries. At least for me, the CMake files don't include transitive dependencies (so linking against Qt5::Widgets is not enough.)