简体   繁体   中英

Runtime error when building SFML with pure CMake

I am starting a project that uses SFML 2.5.1. I am on Windows, and I'm trying to build using pure CMake. The CMake and compilation steps appear to be working fine, but when I try to run my program in CLion, I get the following error:

Process finished with exit code -1073741515

Further, if I find the executable in commandline and attempt to run it there, the program crashes with the following message:

The code execution cannot proceed because sfml-window-d-2.dll was not found.

For context, my project has a file main.cpp in its root directory, as well as my CMakeLists.txt file. The root directory also contains the SFML source code as downloaded from the "Source Code" download link at https://www.sfml-dev.org/download/sfml/2.5.1/ .

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
project(sfmltest)

set(CMAKE_CXX_STANDARD 17)

add_subdirectory(SFML)

include_directories(SFML/include)

add_executable(sfmltest main.cpp)

target_link_libraries(sfmltest sfml-graphics sfml-audio sfml-network sfml-window sfml-system)

Here is my main.cpp. It's just a standard SFML "Hello World" application.

#include <SFML/Window.hpp>

int main()
{
    sf::Window window(sf::VideoMode(800, 600), "My window");
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
    }
    return 0;
}

Am I missing something in my CMakeLists.txt file?

EDIT 1: I don't want a fix such as copy and pasting dll's. I want to be able to build SFML purely from scratch. This is to make sure my project is portable, cross-platform, easy to set up, and compilable with CMake in CLion (NOT Visual Studio or Code::Blocks).

EDIT 2: Adding set(BUILD_SHARED_LIBS FALSE) to my CMakeLists.txt file did the trick. My minimal program is now working!

The file sfml-window-d-2.dll is missing. Add the path to the directory containing SFML DLLs to the Code::Block dynamic library search path (if there is such a thing in C::B) or add it to the PATH environment variable or simply copy those libraries to the folder containing the Executable. You can add a post build step that will copy these libraries.

Edit: I can't comment because of my low reputation.

You can build SFML static libraries to avoid copying dlls. You must disable BUILD_SHARED_LIBS option in CMake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM