3
votes

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).

2

2 Answers

6
votes

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.

2
votes

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.