SFML2, CMake and CLion, app crash - c++

I'm trying to run my project using CLion, SFML2 and CMake. I also placed the .dll in the executable directory.
The lib is located into my project folder.
CMakeLists.txt :
cmake_minimum_required(VERSION 3.3)
project(c__)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(EXECUTABLE_NAME ${PROJECT_NAME})
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/SFML-2.3.2/cmake/Modules/" ${CMAKE_MODULE_PATH})
set(SFML_LIBRARIES "${CMAKE_SOURCE_DIR}/SFML-2.3.2/lib")
set(SFML_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/SFML-2.3.2/include")
set(SOURCE_FILES main.cpp)
add_executable(${EXECUTABLE_NAME} ${SOURCE_FILES})
find_package(SFML COMPONENTS graphics window network audio system REQUIRED)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
Main.cpp
#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;
}
I use mingw x86_x64 and SFML2 64bits.
My app compiles but crash on start.
How can I fix that ?

Related

CLion, MinGW and SDL2: Process finished with exit code -1073741515 (0xC0000135)

I am trying to add SDL2 to my CLion project. I found this guide and tried to follow it while including only SDL2. Everything compiles, but when I start my app I get "Process finished with exit code -1073741515 (0xC0000135)".
In my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.15)
project(Test)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "-lmingw32 -static-libgcc -static-libstdc++")
set(SDL2_PATH "C:/CPP/libs/SDL2-2.0.10/x86_64-w64-mingw32")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "C:/CPP/libs/CMakeModules")
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
if (${SDL2_FOUND})
message(VERBOSE, "sdl found!")
else ()
message(FATAL_ERROR, "sdl not found")
endif ()
message(VERBOSE, ${SDL2_INCLUDE_DIR})
message(VERBOSE, ${SDL2_LIBRARY})
add_executable(Test src/main.cpp)
target_link_libraries(Test ${SDL2_LIBRARY})
main.cpp:
#include <SDL.h>
#include <cstdio>
int main(int argc, char *args[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
}
SDL_Quit();
return 0;
}
I am using CLion 2019.3.2 with bundled CMake, latest MinGW build (x86_64-8.1.0-win32-seh-rt_v6-rev0) and latest SDL2 (2.0.10).
CMake output also looks ok:
VERBOSE,sdl found!
VERBOSE,C:/CPP/libs/SDL2-2.0.10/x86_64-w64-mingw32/include/SDL2
VERBOSE,mingw32-mwindowsC:/CPP/libs/SDL2-2.0.10/x86_64-w64-mingw32/lib/libSDL2main.aC:/CPP/libs/SDL2-2.0.10/x86_64-w64-mingw32/lib/libSDL2.dll.a-lpthread
if you are using visual studio toolchains in CLion:
You need to paste in folder cmake-build-debug or cmake-build-release the files .dll, but no only SDL2_image.dll, all files from folder lib/x86
SDL2_image-devel-2.0.5-VC.zip
My CMakeLists.txt seems different from yours.
Here are my configurations, Hope can help you.
cmake_minimum_required(VERSION 3.15)
project(cppSDL)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLIGS "${CMAKE_CXX_FLAGS} -std=c++17 -lmingw32")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib/x86)
set(SOURCE_FILES SDLtutorial.cpp)
add_executable(SDLtutorial ${SOURCE_FILES})
target_link_libraries(SDLtutorial SDL2main SDL2 SDL2_ttf SDL2_image)

SFML doesn't work with CLion on Windows

I want to run the following simple C++ SFML application in CLion but when I try to do it, I always get the error message Test2.exe has stopped working.
main.cpp
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
This might be a problem with CMake, but I don't get any error message from CLion, so I think SFML is found properly.
CMakeLists.txt
cmake_minimum_required(VERSION 3.9)
project(Test2)
set(CMAKE_CXX_STANDARD 17)
add_executable(Test2 main.cpp)
set(SFML_ROOT "C:/Program Files/SFML-2.4.2")
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules")
find_package(SFML 2 REQUIRED graphics network audio window system)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})
endif()
My SFML is located in C:/Program Files/SFML-2.4.2, and I use the latest version (2.4.2) for MinGW. I have the following MinGW version:
MingGW configuration. My operating system is Windows 8.1 Enterprise.
Separately both CLion and SFML (with Code::Blocks) can work perfectly.
Is there anything I forgot to add to CMakeLists.txt or should I modify some settings in CLion to get SFML to work?
After modifying the CMakeLists.txt in the following way, the created .exe worked without any errors.
cmake_minimum_required(VERSION 3.9)
project(Test2)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc")
set(SFML_STATIC_LIBRARIES TRUE)
add_executable(Test2 main.cpp)
set(SFML_ROOT "C:/Program Files/SFML-2.4.2")
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules")
find_package(SFML 2 REQUIRED graphics network audio window system)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})
endif()

Can't see a new window after running SFML graphics

I trying to test out SFML with Clion following this tutorial. I got to the last part with the SFML example. It compiles with no errors, it sees the SFML, but when I run the actual program all I get is:
C:\Users\yurys\CLionProjects\SFMLDemo\cmake-build-debug\SFMLDemo.exe
Process finished with exit code -1073741515 (0xC0000135)
According to the tutorial I'm suppose to see a new window.
I use MinGW32 with gcc 5.3.0 so my installed version of SFML matches it. I have another version installed to use with Visual Studio 2017.
My CMake files is as follows:
cmake_minimum_required(VERSION 3.6)
project(SFMLDemo)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(SFMLDemo ${SOURCE_FILES})
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules")
find_package(SFML REQUIRED system window graphics network audio)
link_directories("C:/Clion_SFML/") #Path to the SFML Libraries
if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(SFMLDemo ${SFML_LIBRARIES})
endif()
Code:
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(640,480,32),"Hello SFML");
sf::Font font;
font.loadFromFile("OpenSans-Bold.ttf");
sf::Text text("Hello World",font,11);
text.setCharacterSize(32);
text.setPosition(window.getSize().x/2 - text.getGlobalBounds().width/2,
window.getSize().y/2 - text.getGlobalBounds().height/2);
while(window.isOpen()){
sf::Event event;
while(window.pollEvent(event)) {
if(event.type == sf::Event::Closed){
window.close();
}
window.clear(sf::Color::Black);
window.draw(text);
window.display();
}
}
return 0;
}
Answered in comments but I'll submit it as an answer so that it's more visible to others:
In this case, SFML is being linked dynamically. Make sure that the proper .DLLs that are required for SFML are in the same directory as the executable.

Exit code -1073741515 on CLion project with SFML

I'm trying to use SFML on Clion (and MinGW as compiler suite): there isn't any problem during the building and linking process, i can also include SFML files whitout problems but when i run the project i get -1073741515 as exit code.
At the moment my project is only a main.cpp file that i have copied from the sfml tutorial about managing a window
My cmake.txt
cmake_minimum_required(VERSION 3.6)
project(Survival_2D)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Modules" ${CMAKE_MODULE_PATH})
set(SOURCE_FILES main.cpp)
add_executable(Survival_2D ${SOURCE_FILES})
find_package(SFML REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(Survival_2D ${SFML_LIBRARIES})
endif()
main.cpp
#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;
}
Solved by adding sfml-dlls to project folder

How to run SFML in CLion, Error undefined reference to?

I'm new to C++ and try to learn game programming, I choose SFML and run on CLion by Jetbrain and using Ubuntu machine. I following this tutorial SFML and Linux here my code :
#include <SFML/Graphics.hpp>
using namespace sf;
int main() {
RenderWindow window(sf::VideoMode(200, 200), "SFML Work!");
CircleShape shape(100.f);
shape.setFillColor(Color::Green);
while (window.isOpen()) {
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
window.close();
}
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
When I run on CLion it error
CMakeFiles/SFMLBasic.dir/main.cpp.o: In function `main':
undefined reference to `sf::String::String(char const*, std::locale const&)'
undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'
undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
...
undefined reference to `sf::Shape::~Shape()'
How I config or setup to run SFML in CLion, I don't know CMAKE can do that? I only run by terminal, It work if I run this command.
g++ -c main.cpp
g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system
./sfml-app
How to config to use all reference variable without do manual every time in Terminal? Thanks.
After read #Stackia suggestion. This is my solution refer to this tutorial Tutorial: Build your SFML project with CMake
Create a cmake_modules folder and download this file FindSFML.cmake and copy in it.
Edit CMakeLists.txt by add this to the end file, click Reload changes.
# Define sources and executable
set(EXECUTABLE_NAME "MySFML")
add_executable(${EXECUTABLE_NAME} main.cpp)
# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()
Now you can select Executable Name MySFML and click Run (Shift+F10). It Work!
You need to link SFML library in your CMakeLists.txt.
Have a look at CMake target_link_libraries.
And this link may be helpful for you to know how to find SFML library path in CMake.
Here is a FindSFML.cmake module: https://github.com/LaurentGomila/SFML/blob/master/cmake/Modules/FindSFML.cmake
I've fixed it with following steps:
Download http://www.sfml-dev.org/download/sfml/2.3.2/64-Linux
And extract it in folder with my project:
/home/user/ClionProjects/CPP_first/
Named project "CPP_first"
main.cpp contains following
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
CMakeLists.txt contain following:
set(EXECUTABLE_NAME "CPP_first")
add_executable(${EXECUTABLE_NAME} main.cpp)
# Detect and add SFML
set(SFML_DIR "/home/user/ClionProjects/CPP_first/SFML-2.3.2/share/SFML/cmake/Modules")
set(CMAKE_MODULE_PATH "/home/user/ClionProjects/CPP_first/SFML-2.3.2/share/SFML/cmake/Modules" ${CMAKE_MODULE_PATH})
find_package(SFML REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()
Path to my project /home/user/ClionProjects/CPP_first/
PS: I didn't find how to find SFML when it is installed by:
sudo apt-get install libsfml-dev
I hope it will help someone
This is become much simpler now (see this SFML forum discussion).
That's what I did to make my sfml project running in clion:
Added C:\SFML\bin to my env variable Path
Modified CMakeLists.txt of my clion's project:
cmake_minimum_required(VERSION 3.13)
project(Hello_SFML)
set(CMAKE_CXX_STANDARD 14)
find_package(SFML 2.5 COMPONENTS graphics audio REQUIRED)
add_executable(Hello_SFML main.cpp)
target_link_libraries(Hello_SFML sfml-graphics sfml-audio)
If you install SFML by:
sudo apt-get install libsfml-dev
you'll find the path in /usr/share/SFML/