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