I am trying to add two libraries to my project through cmake: libgit2 and git2cpp (a c++ wrapper for libgit2).
Here is my project file structure:
I am trying to add them as libraries in cmake by doing the following:
cmake_minimum_required(VERSION 3.24)
project(create_clib_plugin)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
include_directories("build/include")
add_library(libgit2 SHARED IMPORTED)
set_target_properties(libgit2 PROPERTIES
IMPORTED_LOCATION "C:/Users/minha/CLionProjects/create-clib-plugin/build/lib/libs/libgit2/git2.DLL"
IMPORTED_IMPLIB "C:/Users/minha/CLionProjects/create-clib-plugin/build/lib/libs/libgit2/git2.lib"
INTERFACE_INCLUDE_DIRECTORIES "C:/Users/minha/CLionProjects/create-clib-plugin/build/include"
)
find_library(CPPGIT2_LIBRARY git2cpp HINTS "C:/Users/minha/CLionProjects/create-clib-plugin/build/lib/git2cpp.lib" REQUIRED)
add_executable(create_clib_plugin main.cpp)
TARGET_LINK_LIBRARIES(create_clib_plugin "C:/Users/minha/CLionProjects/create-clib-plugin/build/lib/libs/libgit2/git2.lib")
TARGET_LINK_LIBRARIES(create_clib_plugin "C:/Users/minha/CLionProjects/create-clib-plugin/build/lib/git2cpp.lib")
And finally, my main.cpp
#include "git2cpp/initializer.h"
int main(int argc, char *argv[]) {
auto_git_initializer;
return 0;
}
I'm getting the following errors:
main.obj : error LNK2019: unresolved external symbol "public: __cdecl git::Initializer::Initializer(void)" (??0Initializer#git##QEAA#XZ) referenced in function main [C:\Users\minha\CLionProjects\create-clib-plugin\cmake-build-release\create_clib_plugin.vcxproj]
main.obj : error LNK2019: unresolved external symbol "public: __cdecl git::Initializer::~Initializer(void)" (??1Initializer#git##QEAA#XZ) referenced in function main [C:\Users\minha\CLionProjects\create-clib-plugin\cmake-build-release\create_clib_plugin.vcxproj]
C:\Users\minha\CLionProjects\create-clib-plugin\build\lib\libs\libgit2\git2.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64' [C:\Users\minha\CLionProjects\create-clib-plugin\cmake-build-release\create_clib_plugin.vcxproj]
C:\Users\minha\CLionProjects\create-clib-plugin\build\lib\git2cpp.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64' [C:\Users\minha\CLionProjects\create-clib-plugin\cmake-build-release\create_clib_plugin.vcxproj]
C:\Users\minha\CLionProjects\create-clib-plugin\cmake-build-release\Release\create_clib_plugin.exe : fatal error LNK1120: 2 unresolved externals [C:\Users\minha\CLionProjects\create-clib-plugin\cmake-build-release\create_clib_plugin.vcxproj]
Does anyone have any insight on what could be causing this?
Related
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)
I'm trying to create a project, that will create an executable MyExec.exe, which will depends on another library Niftilib, which itself depends on zlib.
What I did is that I used cmake and created a superbuild, using ExternalProject_Add to download and compile zlib:
ExternalProject_Add(Zlib
SOURCE_DIR "${PROJECT_BINARY_DIR}/deps/zlib"
BINARY_DIR "${PROJECT_BINARY_DIR}/deps/zlib-build"
INSTALL_DIR "${PROJECT_BINARY_DIR}/deps/zlib-install"
GIT_REPOSITORY "${git_protocol}://github.com/madler/zlib.git"
GIT_TAG "50893291621658f355bc5b4d450a8d06a563053d"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DINSTALL_BIN_DIR=<INSTALL_DIR>/bin
-DINSTALL_INC_DIR=<INSTALL_DIR>/include
-DINSTALL_LIB_DIR=<INSTALL_DIR>/lib
-DINSTALL_MAN_DIR=<INSTALL_DIR>/share/man
-DINSTALL_PKGCONFIG_DIR=<INSTALL_DIR>/share/pkgconfig)
I did the same for Nifticlib:
ExternalProject_Add(Nifticlib
SOURCE_DIR "${PROJECT_BINARY_DIR}/deps/nifticlib"
BINARY_DIR "${PROJECT_BINARY_DIR}/deps/nifticlib-build"
INSTALL_DIR "${PROJECT_BINARY_DIR}/deps/nifticlib-install"
GIT_REPOSITORY "${git_protocol}://gitlab.com/slckr/nifticlib.git"
GIT_TAG "9f427b39eb6a16822c6ed34454f87eb751b23220"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DZLIB_INCLUDE_DIR=${ZLIB_INC_DIR}
-DZLIB_LIBRARY_DEBUG=${ZLIB_LIB_DIR}/zlibd.lib
DEPENDS Zlib)
Then I did the same for MyExec:
ExternalProject_Add(MyExec
SOURCE_DIR "${PROJECT_BINARY_DIR}/MyExec"
BINARY_DIR "${PROJECT_BINARY_DIR}/MyExec-build"
INSTALL_DIR "${PROJECT_BINARY_DIR}/MyExec-install"
GIT_REPOSITORY "${git_protocol}://gitlab.com/slckr/MyExec.git"
GIT_TAG "8514aacf5422f1111602e24ea73b229556293b8c"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DBOOST_INC_DIR=${Boost_INCLUDE_DIRS}
-DBOOST_LIB_DIR=${Boost_LIBRARY_DIRS}
-DZLIB_INC_DIR=${ZLIB_INC_DIR}
-DZLIB_LIB_DIR=${ZLIB_LIB_DIR}
-DNIFTI_INC_DIR=${NIFTI_INC_DIR}
-DNIFTI_LIB_DIR=${NIFTI_LIB_DIR}
DEPENDS Nifticlib)
And in my CMakeLists of MyExec project, I have this:
cmake_minimum_required(VERSION 3.6)
project(MyExec)
include_directories(${BOOST_INC_DIR} ${ZLIB_INC_DIR} ${NIFTI_INC_DIR})
link_directories(${BOOST_LIB_DIR} ${ZLIB_LIB_DIR} ${NIFTI_LIB_DIR})
add_subdirectory(DataStructure)
add_subdirectory(FileHandler)
add_subdirectory(Parser)
add_executable(${PROJECT_NAME} MyExec.cc)
target_link_libraries(${PROJECT_NAME} zlibd znz nifticdf niftiio DataStructure FileHandler Parser)
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
My goal is to be able to distribute MyExec.exe without having all the required libraries (except boost). I want nifticlib library to be embedded in my exec.
If I compile this way, it doesn't work, because it says that zlibd.dll is missing. I find out that I had to link my executable to zlibstaticd instead of zlibd.
But my question is, should I also change -DZLIB_LIBRARY_DEBUG=${ZLIB_LIB_DIR}/zlibd.lib to -DZLIB_LIBRARY_DEBUG=${ZLIB_LIB_DIR}/zlibstaticd.lib in Nifticlib project ?
Or does it only matter in the executable project ? Why does nifticlib require me to provide the path to the zlib library, if it's build as a static library and doesn't do any linking ?
Thank you.
Edit: If I dont link the zlib to my executable, I get these errors:
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzdopen referenced in function znzdopen
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzread referenced in function znzread
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzwrite referenced in function znzwrite
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzputs referenced in function znzputs
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzgets referenced in function znzgets
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzputc referenced in function znzputc
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzgetc referenced in function znzgetc
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzflush referenced in function znzflush
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzeof referenced in function znzeof
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzclose referenced in function Xznzclose
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzopen referenced in function znzopen
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzseek referenced in function znzseek
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gztell referenced in function znztell
znz is part of nifticlib. It is the part using the zlib library.
Edit 2: To make it simpler, my question is does the linking happen like Linking 1 or Linking 2 (knowing that nifticlib depends on zlib, both compiled as static libraries):
Linking 1 Linking 2
-------------- -------------- ---------------
| zlib | | zlib | | nifticlib |
-------------- -------------- ---------------
| \ /
-------------- \ /
| nifticlib | \ /
-------------- \ /
| -------------------
-------------- | MyExec.exe |
| MyExec.exe | -------------------
--------------
The usual way is that linking is only done when an executable binary is generated. This includes for example normal executables, but also dynamic libraries (.dll, .so). Linking is not done for static libraries (.lib, .a).
That means if you build a static library of "nifticlib" it does not need to be linked to "zlib".
You should definitly change the dependency from the dynamic one to the static one, because CMake carries dependencies from static libraries over to the final binary. In this case it should not matter though, because you don't specify a real CMake dependency, just some variable for the library.
Note: In Visual Studio you can pretty easily "link" static libraries into each other. What it does is just concatenation of all functions into one .lib file. CMake does not use this feature though and instead carries dependencies over like mentioned above when doing "TARGET_LINK_LIBRARIES" on a static library target.
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.
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.)
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)