How to run SFML in CLion, Error undefined reference to?
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