SFML doesn't work with CLion on Windows - c++

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

Related

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

SFML2, CMake and CLion, app crash

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 ?

SFML not found - CLion/Cmake

I'm basically trying to make an SFML project in CLion which uses CMake.
I first downloaded SFML 2.2 which already had the .dylib files built. It said to install them in usr/local/lib and usr/local/include, but they didn't exist, so I created them and then put the files there.
I then opened up CLion, created a subdirectoy cmake_modules, placed the FindSFML.cmake file inside, and in the root project directory placed these files:
main.cpp
#include <SFML/Graphics.hpp>
int main()
{
sf::err() << "Hello, World!" << std::endl;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.2)
project(HelloWorld)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(HelloWorld ${SOURCE_FILES})
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML COMPONENTS graphics window system REQUIRED)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(HelloWorld ${SFML_Libraries})
The problem is that when I try to build the project, I get the following error:
Undefined symbols for architecture x86_64:
"sf::err()", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I've tried searching for a solution, but none of them seem to be working. Did I put the files in the wrong place, or did I forget some import CMake setting? I know that setting SFML up with Xcode would be easier, but I'd prefer to use CLion if at all possible.
Ok, so I made an entirely new project, ran xcode-select --install on my terminal to make /usr/local/ a directory that gets searched by the compiler, downloaded SFML 2.3 instead of 2.2, decided to use the FindSFML.cmake file shipped with SFML, and made my CmakeLists.txt file look like so:
cmake_minimum_required(VERSION 3.2)
project(SFMLTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(SFMLTest ${SOURCE_FILES})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "~/SFML-2.3/cmake/Modules/")
find_package(SFML REQUIRED graphics window system)
if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(SFMLTest ${SFML_LIBRARIES})
endif(SFML_FOUND)
This compiled and ran the following code in CLion:
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "Window");
while (window.isOpen())
{
sf::Event e;
while (window.pollEvent(e))
{
if (e.type == sf::Event::Closed)
{
window.close();
}
}
window.clear();
window.display();
}
return 0;
}
I don't know what exactly fixed everything, but I'm just glad it did. Hopefully this helps anyone else who's in a similar jam.
For SFML 2.5 and greater a more modern approach has been taken and setting up cmake is easier.
Example CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(SFMLTest)
find_package(SFML 2.5 COMPONENTS graphics audio REQUIRED)
add_executable(SFMLTest main.cpp)
target_link_libraries(SFMLTest sfml-graphics sfml-audio)
Note :
Its no longer needed to setup FindSFML.cmake or SFML_ROOT etc.
SFML_LIBRARIES, SFML_DEPENDENCIES and SFML_INCLUDE_DIR don't exist
anymore
More info https://en.sfml-dev.org/forums/index.php?topic=24070.0
For others who downloaded SFML via brew, I managed to fix this by adding this to my CMakeLists.txt
set(SFML_ROOT /usr/local/lib)

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/