尝试将 sfml 和 c++ 与 Windows 10 上的可移植 vscode 链接起来

Trying to link sfml and c++ with portable vscode on windows 10

本文关键字:可移植 vscode 起来 链接 Windows sfml c++      更新时间:2023-10-16

我正在Windows 10 64位上工作,我正在尝试使用Mingw-w64/32编译器将sfml 2.5.1链接到最新版本的vscode。我安装了c/c ++扩展,python扩展,实时共享和实时共享聊天。我把所有东西都放在我的 USB 闪存盘上,这样我就可以在任何地方处理我的项目。但似乎我无法正确链接 sfml 库。我搜索了很多,但我能找到的所有答案都是针对 Code::Blocks 或只是不起作用。当您由于链接问题而甚至无法编码时,这真的很烦人。

这是我main.cpp

#include <iostream>
#include <string>
#include "SFML/Graphics.hpp"
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
std::cin.get();
return 0;
}

我的launch.json

{
"version": "0.2.0",
"configurations": [
{
"name": "Build",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "F:/Compiler/mingw32/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}

我的tasks.json

{
"tasks": [
{
"type": "shell",
"label": "build",
"command": "F:/Compiler/mingw32/bin/g++.exe",
"args": [
"-Wall",
"-Wextra",
"-g",
"${workspaceFolder}/main.cpp",
"-I${workspaceFolder}/sources/SFML/include",
"-L${workspaceFolder}/sources/SFML/lib",
"-lsfml-audio",
"-lsfml-graphics",
"-lsfml-network",
"-lsfml-system",
"-lsfml-window"
],
"options": {
"cwd": "F:/Compiler/mingw32/bin"
}
}
],
"version": "2.0.0"
}

和我的c_cpp_properties.json

{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"F:/Compiler/mingw32/lib/gcc/i686-w64-mingw32/7.3.0/include/c++/**",
"${workspaceFolder}/sources/SFML/include/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "F:/Compiler/mingw32/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}

我得到一个错误:

Executing task: F:/Compiler/mingw32/bin/g++.exe -Wall -Wextra -g 
F:ProgrammesProjetsRPG/main.cpp - 
IF:ProgrammesProjetsRPG/sources/SFML/include - 
LF:ProgrammesProjetsRPG/sources/SFML/lib -lsfml-audio -lsfml-graphics - 
lsfml-network -lsfml-system -lsfml-window <
C:UserswillyAppDataLocalTempccpyJSRW.o: In function `main':
F:/Programmes/Projets/RPG/main.cpp:7: undefined reference to 
`_imp___ZN2sf6StringC1EPKcRKSt6locale'
F:/Programmes/Projets/RPG/main.cpp:7: undefined reference to 
`_imp___ZN2sf9VideoModeC1Ejjj'
F:/Programmes/Projets/RPG/main.cpp:7: undefined reference to 
`_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15
ContextSettingsE'
F:/Programmes/Projets/RPG/main.cpp:8: undefined reference to 
`_imp___ZN2sf11CircleShapeC1Efj'
F:/Programmes/Projets/RPG/main.cpp:9: undefined reference to 
`_imp___ZN2sf5Color5GreenE'
F:/Programmes/Projets/RPG/main.cpp:9: undefined reference to 
`_imp___ZN2sf5Shape12setFillColorERKNS_5ColorE'
F:/Programmes/Projets/RPG/main.cpp:11: undefined reference to 
`_imp___ZNK2sf6Window6isOpenEv'
F:/Programmes/Projets/RPG/main.cpp:14: undefined reference to 
`_imp___ZN2sf6Window9pollEventERNS_5EventE'
F:/Programmes/Projets/RPG/main.cpp:17: undefined reference to 
`_imp___ZN2sf6Window5closeEv'
F:/Programmes/Projets/RPG/main.cpp:20: undefined reference to 
`_imp___ZN2sf5ColorC1Ehhhh'
F:/Programmes/Projets/RPG/main.cpp:20: undefined reference to 
`_imp___ZN2sf12RenderTarget5clearERKNS_5ColorE'
F:/Programmes/Projets/RPG/main.cpp:21: undefined reference to 
`_imp___ZN2sf12RenderStates7DefaultE'
F:/Programmes/Projets/RPG/main.cpp:21: undefined reference to 
`_imp___ZN2sf12RenderTarget4drawERKNS_8DrawableERKNS_12RenderStatesE'
F:/Programmes/Projets/RPG/main.cpp:22: undefined reference to 
`_imp___ZN2sf6Window7displayEv'
F:/Programmes/Projets/RPG/main.cpp:7: undefined reference to 
`_imp___ZN2sf12RenderWindowD1Ev'
F:/Programmes/Projets/RPG/main.cpp:7: undefined reference to 
`_imp___ZN2sf12RenderWindowD1Ev'
C:UserswillyAppDataLocalTempccpyJSRW.o: In function 
`ZN2sf11CircleShapeD1Ev':
F:/Programmes/Projets/RPG/sources/SFML/include/SFML/Graphics/
CircleShape.hpp:41: undefined reference to `_imp___ZTVN2sf11CircleShapeE'
F:/Programmes/Projets/RPG/sources/SFML/include/SFML/Graphics/
CircleShape.hpp:41: undefined reference to `_imp___ZTVN2sf11CircleShapeE'
F:/Programmes/Projets/RPG/sources/SFML/include/SFML/Graphics/
CircleShape.hpp:41: undefined reference to `_imp___ZN2sf5ShapeD2Ev'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.

好的,基本上我只是下载了错误版本的 SFML。我下载了SFML Visual C++ 15 (2017) - 32-bit但我应该采取的是SFML GCC 7.3.0 MinGW (DW2) - 32-bit.我还必须在tasks.json中精确地.exe文件的名称:

"-o",
"${workspaceFolder}/a.exe",