如何在CLion中运行SFML,错误为undefined reference to?

7

我刚接触C++并想学习游戏编程,我选择了SFML,并在Jetbrain的CLion上使用Ubuntu机器运行。我正在遵循这个教程SFML and Linux,这是我的代码:

#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;
}

当我在CLion上运行时出现错误

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

我该怎样在CLion中配置或安装SFML?我不知道CMAKE是否能做到这一点?我只能通过终端运行,如果我运行这个命令,它就可以工作。

g++ -c main.cpp
g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system
./sfml-app

如何配置才能在终端中使用所有引用变量,而不需要手动操作?谢谢。
5个回答

7
在阅读 @Stackia 的建议后,我参考了这篇教程: 教程:使用 CMake 构建 SFML 项目,并制定了以下解决方案:
  1. 创建一个名为 cmake_modules 的文件夹,并下载此文件 FindSFML.cmake 并将其复制到该文件夹中。

  2. 通过将以下内容添加到文件末尾来编辑 CMakeLists.txt,然后单击“重新加载更改”。

<br>
 # 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. 现在您可以选择可执行文件名MySFML,然后单击运行(Shift + F10)。它可以工作! enter image description here

我还会添加${SFML_DEPENDENCIES}。在链接共享库时不会产生任何问题,并且在切换到静态链接时会自动添加所有SFML的依赖项。 - Lukas

4
我已经通过以下步骤来修复它:
  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:我没有找到如何在以下情况下查找已安装的SFML:

sudo apt-get install libsfml-dev

我希望这能对某些人有所帮助


4

谢谢,我会去查看的。 - Phonbopit
我刚才发现我不小心登录了另一个账户。这个问题是我创建的,在阅读了您的建议后,我尝试进行修复,现在它已经完美解决了,谢谢。 - Phonbopit

2

现在这个过程变得更加简单了(请参见此SFML论坛讨论)。 以下是我在CLion中运行我的SFML项目所做的:

最初的回答:

  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)
    

1
这在Linux上完美运行。SFML通过sudo apt install libsfml-dev安装。在Linux上,您甚至不需要步骤1。 - Tommimon

0

如果您通过以下方式安装SFML:

sudo apt-get install libsfml-dev

你可以在 /usr/share/SFML/ 找到该路径。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接