如何在Linux Ubuntu中设置SFML库?

时间:2018-07-30 14:41:47

标签: c++ linux cmake sfml clion

我正在Linux上使用CLion IDE,它要求使用cmake进行开发。我遵循了this来获取以下代码。

我有以下示例代码:

#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如下:

cmake_minimum_required(VERSION 3.12)
project(SFML_codes)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SFML_INCLUDE_DIR "/home/user/Desktop/SFML-2.5.0/include")
set(SFML_LIBRARY_DIR "/home/user/Desktop/SFML-2.5.0/lib")
link_directories("/home/user/Desktop/SFML-2.5.0/lib")
include_directories("/home/user/Desktop/SFML-2.5.0/include")
set(SFML_DIR "/home/user/Desktop/SFML-2.5.0/lib/cmake/SFML")
find_package(SFML 2.5.0 COMPONENTS system window graphics network audio)

if(SFML_FOUND)

    message(STATUS "SFML_INCLUDE_DIRS: ${SFML_INCLUDE_DIR}")
    message(STATUS "SFML_LIBRARIES: ${SFML_LIBRARIES}")
    message(STATUS "SFML_VERSION: ${SFML_VERSION}")

    include_directories(${SFML_INCLUDE_DIRS})

endif()
add_executable(SFML_codes main.cpp)

target_link_libraries (SFML_codes sfml-graphics sfml-window sfml-system)

输出:

free(): invalid pointer

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

经过很多努力来消除链接器错误,我才能够成功编译。但是在运行代码时,我收到一个错误,试图释放/删除不是指针的内容。我调试了一下,它似乎是main方法中导致此错误的第一行。根据上面的链接,它应该运行良好。

令我惊讶的是,以下代码运行良好:

#include <SFML/System.hpp>
#include <iostream>

int main()
{
    sf::Clock Clock;
    while (Clock.getElapsedTime().asSeconds() < 5.f)
    {
        std::cout << Clock.getElapsedTime().asSeconds() << std::endl;
        sf::seconds(0.5f);
    }

    return 0;
}

我在做什么错了?

0 个答案:

没有答案