Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 931
Phonbopit Avatar asked Oct 28 '14 06:10

Phonbopit


1 Answers

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
like image 169
Phonbopit Avatar answered Nov 09 '22 20:11

Phonbopit