簡體   English   中英

致命錯誤:找不到SFML / System.hpp文件

[英]fatal error: SFML/System.hpp' file not found

我正在嘗試使用SMFL,CMAKE和VSCode。 Cmake無法鏈接這些SFML文件。 我使用Homebrew安裝SFML和CMake。

我試過將SMFL_ROOTSFML_INCLUDE_DIR包含在SFML文件中。

cmake_minimum_required(VERSION 3.14.2)
project(WordTypeCpp VERSION 0.1.0 )

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
set(CMAKE_CXX_STANDARD 17)

find_package(SFML REQUIRED COMPONENTS graphics system)
if (SFML_FOUND)
    set(SFML_ROOT "/usr/local/Cellar/sfml/2.5.1/include/SFML/")
# include_directories(${SFML_INCLUDE_DIR} "/usr/local/Cellar/sfml/2.5.1/include")
endif(SFML_FOUND)

set(SOURCE_FILES Main.cpp)
add_executable(Main ${SOURCE_FILES})

target_link_libraries(Main ${SFML_LIBRARIES})

add_definitions(-include ${CMAKE_CURRENT_SOURCE_DIR}/PCH.hpp)

include(CPack)

我的Main.cpp文件

#include "PCH.hpp"
#include "Main.hpp"

int main()
{
    std::cout << "SFML is running!" << '\n';

    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
#ifdef SFML_SYSTEM_WINDOWS
    __windowsHelper.setIcon(window.getSystemHandle());
#endif

    sf::CircleShape shape(window.getSize().x / 2);
    shape.setFillColor(sf::Color::White);

    sf::Texture shapeTexture;
    shapeTexture.loadFromFile("content/sfml.png");
    shape.setTexture(&shapeTexture);

    sf::Event event;

    while (window.isOpen())
    {
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}
// PCH.hpp file
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>

--config Debug --target Main -- -j 6
[build] [1/2  50% :: 1.825] Building CXX object src/CMakeFiles/Main.dir/Main.cpp.o
[build] FAILED: src/CMakeFiles/Main.dir/Main.cpp.o 
[build] /usr/bin/clang++    -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk   -include /Users/.../WordTypeCpp/src/PCH.hpp -std=gnu++1z -MD -MT src/CMakeFiles/Main.dir/Main.cpp.o -MF src/CMakeFiles/Main.dir/Main.cpp.o.d -o src/CMakeFiles/Main.dir/Main.cpp.o -c ../src/Main.cpp
[build] In file included from <built-in>:1:
[build] /Users/.../WordTypeCpp/src/PCH.hpp:11:10: fatal error: 'SFML/System.hpp' file not found
[build] #include <SFML/System.hpp>
[build]          ^~~~~~~~~~~~~~~~~
[build] 1 error generated.
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1

錯誤在於此行:

target_link_libraries(Main ${SFML_LIBRARIES})

這不會正確傳播需求。 它只會鏈接sfml庫。 通常,您還需要其他要求,例如包含目錄。

相反,您應該鏈接到sfml導出的目標:

target_link_libraries(Main PRIVATE sfml-graphics sfml-system)

另外,您不需要:

# uneeded
if (SFML_FOUND)
    set(SFML_ROOT "/usr/local/Cellar/sfml/2.5.1/include/SFML/")
    # include_directories(${SFML_INCLUDE_DIR} "/usr/local/Cellar/sfml/2.5.1/include")
endif(SFML_FOUND)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM