gpt4 book ai didi

c++ - fatal error : SFML/System. 找不到 hpp 文件

转载 作者:行者123 更新时间:2023-11-28 04:18:46 43 4
gpt4 key购买 nike

我正在尝试使用 SMFL、CMAKE 和 VSCode。 Cmake 无法链接这些 SFML 文件。我使用 Homebrew 安装 SFML & CMake。

我已尝试将 SFML 文件包含在 SMFL_ROOTSFML_INCLUDE_DIR 中,但无济于事。

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)

关于c++ - fatal error : SFML/System. 找不到 hpp 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55973407/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com