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

不问归期 提交于 2020-07-17 10:47:31

问题


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.


回答1:


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




回答2:


After read @Stackia suggestion. This is my solution refer to this tutorial Tutorial: Build your SFML project with CMake

  1. Create a cmake_modules folder and download this file FindSFML.cmake and copy in it.

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

  1. Now you can select Executable Name MySFML and click Run (Shift+F10). It Work! enter image description here



回答3:


I've fixed it with following steps:

  1. 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/
    
  2. Named project "CPP_first"

  3. 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;
    }
    
  4. 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()
    
  5. 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




回答4:


This is become much simpler now (see this SFML forum discussion). That's what I did to make my sfml project running in clion:

  1. Added C:\SFML\bin to my env variable Path
  2. 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)
    



回答5:


If you install SFML by:

sudo apt-get install libsfml-dev

you'll find the path in /usr/share/SFML/



来源:https://stackoverflow.com/questions/26602471/how-to-run-sfml-in-clion-error-undefined-reference-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!