gpt4 book ai didi

c++ - Visual Studio代码SFML没有这样的文件或目录

转载 作者:行者123 更新时间:2023-12-01 16:42:50 27 4
gpt4 key购买 nike

您好,我一直在尝试使用 Visual Studo 代码来学习 C++ SFML。在观看了如何在 C++ 中安装 sfml 的教程后,我已经准备好了一切。但是,问题是当我尝试编译时它给了我这个错误:“main.cpp:2:10: fatal error :SFML/Graphics.hpp:没有这样的文件或目录”。我已经阅读了很多指南,但似乎没有一个起作用。

这是我的代码:

#include <SFML/Graphics.hpp>
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(1280,720),"Nareszcie");
while(window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type==sf::Event::Closed)
{
window.close();

}
window.clear();
}
}
return 0;
}

希望有人能帮我解决这个问题。

tasks.json:

{
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:/mingw32/bin/g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:/mingw32/bin"
}
},
{
"type": "shell",
"label": "cpp.exe build active file",
"command": "C:\\mingw32\\bin\\cpp.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\mingw32\\bin"
}
}
],
"version": "2.0.0"
}

c_cpp_prperties.json:

{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/SFML-2.5.1/include/**",
"C:/SFML-2.5.1"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/mingw32/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}

最佳答案

您还需要将包含目录添加到构建说明中。

"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"C:/SFML-2.5.1/include/"
],

但是,您还需要将可执行文件链接到 SFML 库。

"args": [
"-g",
"${file}",
"C:/path/to/sfml/libsfml-graphic.a" // something like that
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"C:/SFML-2.5.1/include/",
],

我的建议是使用适当的构建系统,例如 CMake、meson 或其他系统,它们会自动执行此类操作。这是一个 CMake 示例:

cmake_minimum_required(VERSION 3.14)
project(your-project CXX)

# creates your executable with two cpp file in it
add_executable(my-exe main.cpp otherfile.cpp)

# find sfml. Assume the command line argument -DCMAKE_PREFIX_PATH="C:/SFML-2.5.1"
find_package(SFML 2.5.1 COMPONENTS graphic REQUIRED)

# configure your project correctly. Take care of include directories and linking
target_link_libraries(my-exe PUBLIC sfml-graphic)

VSCode 还为这些构建系统提供了一个很好的插件。

关于c++ - Visual Studio代码SFML没有这样的文件或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59327010/

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