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

I am new to C ++ and try to learn game programming, I choose SFML and run on CLion using Jetbrain and using the Ubuntu machine. I follow this SFML and Linux tutorial here my code is:

#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 CLion, this is an 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 do I configure or configure SFML to run in CLion, I don’t know that CMAKE can do this? I only run the terminal, It works 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 configure to use the entire reference variable without manual input to the terminal each time? Thanks.

+6
source share
5 answers

You need to link the SFML library with your CMakeLists.txt.

Take a look at CMake target_link_libraries .

And this link may be useful for you to find out how to find the path of the SFML library in CMake.

Here is the FindSFML.cmake module: https://github.com/LaurentGomila/SFML/blob/master/cmake/Modules/FindSFML.cmake

+2
source

After reading @Stackia's suggestion. This is my solution for this tutorial Tutorial: Create an SFML Project Using CMake

  • Create the cmake_modules folder and download this FindSFML.cmake file and copy it.

  • Modify CMakeLists.txt , adding this to the final file, click Update 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() 

code>
  1. Now you can select the MySFML executable name and click Run (Shift + F10). It works! enter image description here
+6
source

I fixed it with the following steps:

  • Download http://www.sfml-dev.org/download/sfml/2.3.2/64-Linux

    And extract it to the folder with my project:

     /home/user/ClionProjects/CPP_first/ 
  • Named project "CPP_first"

  • main.cpp contains the 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 the 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() 
  • The path to my project /home/user/ClionProjects/CPP_first/

PS: I did not find how to find SFML when it is installed:

 sudo apt-get install libsfml-dev 

I hope this helps someone

+3
source

If you install SFML:

 sudo apt-get install libsfml-dev 

you will find the path in / usr / share / SFML /

0
source

Now it has become much easier (see the SFML forum discussion ). Here is what I did to get my sfml project working in clion:

  1. Added C:\SFML\bin to my env Path variable
  2. Modified CMakeLists.txt my clion 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) 
0
source

Source: https://habr.com/ru/post/1205664/


All Articles