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.
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 want to use taglib in my QT project on windows, so i build taglib and zlib from source like below:
cmake CMakeLists.txt
cmake --build --config release
cmake command uses visual studio compiler, i build zlib and taglib from source with above command with no error, i added their include and lib path to QT project .pro file like below:
INCLUDEPATH+=F:\project\TAGLIB\taglib\taglib F:\project\TAGLIB\taglib\taglib\toolkit F:\project\TAGLIB\taglib F:\project\zlib-1.2.11\Release F:\project\zlib-1.2.11
LIBS+=-LF:\project\zlib-1.2.11\Release -lzlib
LIBS+= -LF:\project\TAGLIB\taglib\taglib\Release -ltag
LIBS+=-LF:\project\TAGLIB\taglib\taglib\Debug -ltag
LIBS+=-LF:\project\zlib-1.2.11\Debug -lzlibd
and in main.cpp i added:
#include<fileref.h>
#include<tag.h>
#include<QDebug>
and in main function:
TagLib::FileRef f("C:\\Users\\sub\\Downloads\\Mohsen Chavoshi - Sharhe Alef.mp3");
TagLib::String artist = f.tag()->artist();
when i run my application in debug mode,it doesn't run and give me below errors:
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl TagLib::String::~String(void)" (__imp_??1String#TagLib##UEAA#XZ) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: char const * __cdecl TagLib::String::toCString(bool)const " (__imp_?toCString#String#TagLib##QEBAPEBD_N#Z) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl TagLib::FileName::FileName(char const *)" (__imp_??0FileName#TagLib##QEAA#PEBD#Z) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl TagLib::FileRef::FileRef(class TagLib::FileName,bool,enum TagLib::AudioProperties::ReadStyle)" (__imp_??0FileRef#TagLib##QEAA#VFileName#1#_NW4ReadStyle#AudioProperties#1##Z) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl TagLib::FileRef::~FileRef(void)" (__imp_??1FileRef#TagLib##UEAA#XZ) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: class TagLib::Tag * __cdecl TagLib::FileRef::tag(void)const " (__imp_?tag#FileRef#TagLib##QEBAPEAVTag#2#XZ) referenced in function main
debug\testTag2.exe:-1: error: LNK1120: 6 unresolved externals
but when i run application in Release mode,the above error are just some Warning and application run with no problem, why this is happening?
thank you.
I'm getting a lot of erros LNK2019, after I use the nmake command on VS2015 console:
kmeans.cpp.obj : error LNK2019: unresolved external symbol clGetPlatformIDs > referenced in function "int __cdecl initialize(int)" (?initialize##YAHH#Z) kmeans.cpp.obj : error LNK2019: unresolved external symbol clCreateContextFromType referenced in function "int __cdecl initialize(int)" (?initialize##YAHH#Z) kmeans.cpp.obj : error LNK2019: unresolved external symbol clReleaseContext referenced in function "int __cdecl shutdown(void)" (?shutdown##YAHXZ) kmeans.cpp.obj : error LNK2019: unresolved external symbol clGetContextInfo referenced in function "int __cdecl initialize(int)" (?initialize##YAHH#Z) kmeans.cpp.obj : error LNK2019: unresolved external symbol clCreateCommandQueue referenced in function "int __cdecl initialize(int)" (?initialize##YAHH#Z) kmeans.cpp.obj : error LNK2019: unresolved external symbol clReleaseCommandQueue referenced in function "int __cdecl shutdown(void)" (?shutdown##YAHXZ)
My PC Specs and OS:
Windows 10 x64
Core i7 4790k
GTX 1080.
NVidia CUDA SDK Installed.
Does anybody know how to fix it, or faced the same problem? Already tried the #pragma with OpenCL Lib but that didnt work.
Already tried to change application type from Windows to console on VS, but that didn't work too.
My CMakeLists.txt is:
cmake_minimum_required( VERSION 2.6 )
project( kmeans )
find_package( OpenCL REQUIRED )
include_directories(${OpenCL_INCLUDE_DIRS})
add_executable( kmeans kmeans.cpp )
target_link_libraries( kmeans ${OPENCL_LIBRARIES} )
Link of the code repository that I'm using:
https://github.com/davidebaltieri31/OpenCL_kMeans
My exact steps:
Open VS2015 console.
Type "cmake . -G "NMake Makefiles""
Type nmake.
Then I get the LNK2019 Erros (Full screenshot here: http://puu.sh/uNOWQ/66e8d134a1.png).
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)