我一直在尝试将 SFML 库与 Visual Studio Code 和 C/C++ v1.19.9 Microsoft 扩展一起使用。我的目标是配置 Visual Studio Code 以使用“运行代码”按钮直接从编辑器编译和运行 SFML 代码。
我从一个简单的代码开始打开一个空窗口:
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Window");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(); // Clear the window
// Draw your graphics here
window.display(); // Display what was drawn
}
return 0;
}
最初,我使用以下命令通过终端编译它:
/usr/bin/g++ \
-fdiagnostics-color=always \
-g \
main.cpp \
-o main \
-I/opt/homebrew/Cellar/sfml/2.6.1/include \
-L/opt/homebrew/Cellar/sfml/2.6.1/lib \
-lsfml-graphics \
-lsfml-window \
-lsfml-system \
-lsfml-network
效果很好,程序编译并运行成功。但是,我想通过配置 launch.json、tasks.json 和 c_cpp_proppieties.json 文件来简化流程,以便直接在 Visual Studio Code 中进行编辑、编译和执行。
这就是我所做的:
在 c_cpp_proppieties.json 中,我添加了 SFML 包含文件夹的路径,以帮助 Visual Studio Code 和 C/C++ 扩展理解语法:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/opt/homebrew/Cellar/sfml/2.6.1/include"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-arm64"
}
],
"version": 4
}
然后在tasks.json中,我定义了一个任务来使用SFML构建代码:
{
"version": "2.0.0",
"tasks": [
{
"label": "C/C++: g++ build active file with SFML",
"type": "shell",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I/opt/homebrew/Cellar/sfml/2.6.1/include",
"-L/opt/homebrew/Cellar/sfml/2.6.1/lib",
"-lsfml-graphics",
"-lsfml-window",
"-lsfml-system",
"-lsfml-network"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"],
"detail": "Task generated by Debugger."
}
]
}
在 launch.json 文件中,我更新了“preLaunchTask”以使用tasks.json 中定义的任务:
{
"configurations": [
{
"name": "C/C++: clang build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang build active file"
},
{
"name": "C/C++: g++ build and debug active file with SFML",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: g++ build active file with SFML"
}
],
"version": "2.0.0"
}
但是,尽管进行了这些配置,我在尝试运行代码时遇到了以下错误:
[Running] cd "/Users/david/Desktop/c++ project/hello world sfml/" && g++ main.cpp -o main && "/Users/david/Desktop/c++ project/hello world sfml/"main
main.cpp:1:10: fatal error: 'SFML/Graphics.hpp' file not found
#include <SFML/Graphics.hpp>
^~~~~~~~~~~~~~~~~~~
1 error generated.
[Done] exited with code=1 in 0.052 seconds
到目前为止我还没有找到解决方案。有人对可能缺少的内容有任何见解吗?预先感谢您的帮助。
我尝试使用@starball提供的解决方案,但遇到了“.run”扩展名的问题,我不想使用它。因此,我卸载了它并选择了 cpptools 扩展中包含的“按钮”,如前面的答案中所建议的。然而,问题仍然存在。为了解决这个问题,我重新安装了 C/C++ v1.19.9 Microsoft 扩展并调整了“task.json”和“launch.json”文件。这是我现在的任务:
{
"tasks": [
// Task for building C/C++ code with SFML
{
"label": "C/C++: g++ build active file with SFML", // Name of the task
"type": "shell", // Specifies the type of task as a shell command
"command": "/usr/bin/g++", // Command to execute
"args": [
"-fdiagnostics-color=always", // Enable colored diagnostics
"${file}", // Placeholder for the current active file
"-o", // Output flag
"${fileDirname}/${fileBasenameNoExtension}", // Output filename based on directory and base filename
"-I/opt/homebrew/Cellar/sfml/2.6.1/include", // Include SFML headers
"-L/opt/homebrew/Cellar/sfml/2.6.1/lib", // Specify SFML library directory
"-lsfml-graphics", // Link SFML graphics library
"-lsfml-window", // Link SFML window library
"-lsfml-system", // Link SFML system library
"-lsfml-network" // Link SFML network library
],
"group": "build", // Task group (build)
"presentation": {
"reveal": "silent", // Do not show the output when this task is executed
"echo": false, // Do not echo the executed command in the terminal
"focus": false, // Do not bring focus to the terminal when this task is executed
"panel": "shared" // Show the output in a shared terminal panel
}
}
],
"version": "2.0.0" // Version of the tasks configuration
}
以及更新的启动配置:
{
"configurations": [
// Configuration for building and running C/C++ code with SFML
{
"name": "C/C++: Build and Run", // Name of the configuration
"type": "cppdbg", // Type of debugger (C/C++ debugger)
"request": "launch", // Specifies that this configuration launches the debugger
"program": "${fileDirname}/${fileBasenameNoExtension}", // Path to the executable to debug
"args": [], // Arguments to pass to the executable
"stopAtEntry": false, // Whether to stop at the entry point of the program
"cwd": "${fileDirname}", // Current working directory for the debugger
"environment": [], // Environment variables to set for the debugger
"externalConsole": false, // Whether to use an external console for input/output
"MIMode": "lldb", // Debugger mode (LLDB in this case)
"preLaunchTask": "C/C++: g++ build active file with SFML" // Task to run before launching the debugger
}
],
"version": "2.0.0" // Version of the configurations
}
除了配置以消除调试过程之外,我还删除了我真正不需要的任何其他任务。
致@sweenish 和@André Lehto,我将来会尝试CMake。谢谢你的推荐。