您好,我一直在尝试使用可视化的studo代码学习c ++ SFML。看完教程如何在c ++中安装sfml之后,我已完成了所有设置。但是,问题是当我尝试编译时,它给了我这个错误:“ main.cpp:2:10:致命错误: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
}
您也需要将include目录添加到构建说明中。
"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,介子或其他,它们会自动执行此类操作。这是一个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也为这些构建系统提供了一个不错的插件。
我将路径添加到task.json文件这是它的外观:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:/mingw32/bin/g++.exe",
"args": [
"-g",
"${file}",
"C:/SFML-2.5.1/lib/libsfml-graphic.a",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"C:/SFML-2.5.1/include"
],
"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"
}
}
]
}
并且仍然无法正常工作。