hellomyprogram 发表于 2023-7-22 18:24:41

怎么构建VSCode+MinGW+SFML环境

本帖最后由 hellomyprogram 于 2023-7-22 18:51 编辑

我在按照网上的教程构建完环境之后,运行时遇到了这样的问题:
D:/NoSpacePrograms/MinGW/bin/g++.exe -fdiagnostics-color=always -g "项目目录/*.cpp" -o "项目目录/.vscode/program.exe" -I D:/NoSpacePrograms/MinGW-SFML/include -lsfml-graphics -lsfml-system -lsfml-window
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-graphics.a when searching for -lsfml-graphics
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib\libsfml-graphics.a when searching for -lsfml-graphics
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/../lib/libsfml-graphics.a when searching for -lsfml-graphics
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/../lib\libsfml-graphics.a when searching for -lsfml-graphics
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-graphics.a when searching for -lsfml-graphics
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib\libsfml-graphics.a when searching for -lsfml-graphics
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-graphics.a when searching for -lsfml-graphics
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/../lib/libsfml-graphics.a when searching for -lsfml-graphics
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-graphics.a when searching for -lsfml-graphics
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lsfml-graphics
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-system.a when searching for -lsfml-system
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib\libsfml-system.a when searching for -lsfml-system
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/../lib/libsfml-system.a when searching for -lsfml-system
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/../lib\libsfml-system.a when searching for -lsfml-system
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-system.a when searching for -lsfml-system
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib\libsfml-system.a when searching for -lsfml-system
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-system.a when searching for -lsfml-system
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/../lib/libsfml-system.a when searching for -lsfml-system
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-system.a when searching for -lsfml-system
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lsfml-system
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-window.a when searching for -lsfml-window
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib\libsfml-window.a when searching for -lsfml-window
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/../lib/libsfml-window.a when searching for -lsfml-window
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/../lib\libsfml-window.a when searching for -lsfml-window
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-window.a when searching for -lsfml-window
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib\libsfml-window.a when searching for -lsfml-window
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-window.a when searching for -lsfml-window
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/../lib/libsfml-window.a when searching for -lsfml-window
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-window.a when searching for -lsfml-window
D:/NoSpacePrograms/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lsfml-window
collect2.exe: error: ld returned 1 exit status
我的MinGW版本为7.3.0,SFML为2.5.1。
以下是我按照网上教程构建的时候创建的“c_cpp_properties.json”文件:
{
    "configurations": [
      {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/NoSpacePrograms/MinGW/include",
                "D:/NoSpacePrograms/MinGW-SFML/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:/NoSpacePrograms/MinGW/bin/gcc.exe",
            "cStandard": "c17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "windows-gcc-x64"
      }
    ],
    "version": 4
}
还有“tasks.json”文件:
{
    "tasks": [
      {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",
            "command": "D:/NoSpacePrograms/MinGW/bin/g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${fileDirname}/*.cpp",
                "-o",
                "${fileDirname}/.vscode/program.exe",
                "-I",
                "D:/NoSpacePrograms/MinGW-SFML/include",
                "-lsfml-graphics",
                "-lsfml-system",
                "-lsfml-window"
            ],
            "options": {
                "cwd": "D:/NoSpacePrograms/MinGW/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
      }
    ],
    "version": "2.0.0"
}
VSCode并没有对我的这行代码进行警告,但是编译器产生的错误指向了这一行代码:
#include <SFML/Graphics.hpp>
我已经向用户环境变量PATH添加了MinGW和SFML的bin路径,向用户环境变量CPLUS_INCLUDE_PATH和LIBRARY_PATH分别添加了SFML的include路径和lib路径。
错误信息似乎提到不存在-lsfml之类的参数,但是网上的教程都是加了这些参数的,去掉参数照样报错。
根据“skipping incompatible”一则消息,编译器似乎觉得SFML的源文件是不可取的。
我参考的教程是
https:// www .xjx100.cn/news/435339.html

Mike_python小 发表于 2023-7-22 19:00:57

从错误信息来看,编译器在链接阶段找不到SFML的库文件。这可能是因为你使用的MinGW和SFML版本不兼容导致的。

首先,请确保你下载并安装了与你的MinGW版本兼容的SFML库。例如,对于MinGW 7.3.0,你需要下载SFML 2.5.1 for MinGW 7.3.0。确保你的SFML库文件是与你的MinGW版本匹配的。

接下来,检查一下你的环境变量设置是否正确。确保将SFML库的bin路径添加到系统的PATH环境变量中。

另外,在你的"tasks.json"文件中,尝试将"-lsfml-graphics"、"-lsfml-system"和"-lsfml-window"这些参数改成具体的库文件路径,而不是使用"-l"选项。例如:


"args": [
    "-fdiagnostics-color=always",
    "-g",
    "${fileDirname}/*.cpp",
    "-o",
    "${fileDirname}/.vscode/program.exe",
    "-I",
    "D:/NoSpacePrograms/MinGW-SFML/include",
    "D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-graphics.a",
    "D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-system.a",
    "D:/NoSpacePrograms/MinGW-SFML/lib/libsfml-window.a"
]


请根据你实际安装SFML的路径进行修改。

最后,保存并重新运行你的程序。如果问题仍然存在,请检查编译器输出的详细错误信息,看看是否有其他引起链接问题的原因。

isdkz 发表于 2023-7-22 19:13:25

看起来你遇到的问题是由于链接器找不到与你的 MinGW 环境兼容的 SFML 库文件。你需要确保下载的 SFML 版本与你的 MinGW 的架构(32位或64位)和版本匹配。如果 MinGW 是64位的,那么 SFML 库也必须是64位的。反之亦然,如果你的 MinGW 是32位的,你需要使用32位的 SFML 库。

你可以按照以下步骤进行操作:

1. 首先,确定你的 MinGW 是32位还是64位。你可以通过在终端中运行gcc -v来查看。在最后一行,如果看到 "Target: x86_64-w64-mingw32",那么你的 MinGW 是64位的。如果看到 "Target: i686-w64-mingw32",那么你的 MinGW 是32位的。

2. 一旦你知道了 MinGW 的架构,你就可以下载相应版本的 SFML 库。你可以从 SFML 官方网站的下载页面下载。确保选择的 SFML 库与你的 MinGW 架构匹配。

3. 下载并解压 SFML 库后,将其复制到你的 MinGW 安装目录中。例如,如果你的 MinGW 安装在 "D:/NoSpacePrograms/MinGW",你可以将 SFML 库复制到 "D:/NoSpacePrograms/MinGW-SFML"。

4. 在你的 "tasks.json" 文件中,确保-lsfml-graphics , -lsfml-system和-lsfml-window参数出现在链接器参数列表的最后。链接器参数应该如下所示:
   
{
    "type": "cppbuild",
    "label": "C/C++: gcc.exe 生成活动文件",
    "command": "D:/NoSpacePrograms/MinGW/bin/g++.exe",
    "args": [
      "-fdiagnostics-color=always",
      "-g",
      "${fileDirname}/*.cpp",
      "-o",
      "${fileDirname}/.vscode/program.exe",
      "-I",
      "D:/NoSpacePrograms/MinGW-SFML/include",
      "-L",
      "D:/NoSpacePrograms/MinGW-SFML/lib",
      "-lsfml-graphics",
      "-lsfml-window",
      "-lsfml-system"
    ],
    ...
}

在这里, -L参数指定了库文件的路径, -l参数告诉链接器链接哪些库。

5. 最后,确保你的项目中包含了 SFML 的头文件,并且你正确地使用了 SFML 的命名空间和类。例如:

#include <SFML/Graphics.hpp>

int main() {
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
    // ...
    return 0;
}


希望这些信息对你有所帮助,如果你还有其他问题,欢迎继续提问。

hellomyprogram 发表于 2023-7-22 22:06:31

isdkz 发表于 2023-7-22 19:13
看起来你遇到的问题是由于链接器找不到与你的 MinGW 环境兼容的 SFML 库文件。你需要确保下载的 SFML 版本 ...

ChatGPT还是有点用的(

歌者文明清理员 发表于 2023-7-23 22:33:08

hellomyprogram 发表于 2023-7-22 22:06
ChatGPT还是有点用的(

小甲鱼有教程
页: [1]
查看完整版本: 怎么构建VSCode+MinGW+SFML环境