带有 VS Code 和 MSVC cl.exe LNK2019 错误的 CSFML

CSFML with VS Code and MSVC cl.exe LNK2019 error

所以我是 C 语言的新手,开始研究 CSFML 并在尝试在 VS Code 中编译时遇到 运行 问题这是我的 tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\${fileBasenameNoExtension}.exe",
                "${file}",
                "-I${fileDirname}\CSFML-2.5-windows-64-bit\include",
                "-L${fileDirname}\CSFML-2.5-windows-64-bit\lib\msvc"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

这是我的 launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "cl.exe - Build and debug active file",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${fileDirname}\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "preLaunchTask": "C/C++: cl.exe build active file"
        }
    ]
}

这里是一些简单的代码,我正在尝试 运行 只是制作一个 window:

#include <stdio.h>
#include <SFML/Graphics.h>
int main(int argc, char const *argv[])
{
    /*24 bit colour depth at 800 by 600 resolution*/
    sfVideoMode mode = {800, 600, 24};
    sfRenderWindow *window;
    /*This will be used in the main loop*/
    sfEvent event;
    /*Start creating the window*/
    window = sfRenderWindow_create(mode, "SFML Test", sfResize | sfClose, NULL);
    if (!window)
    {
        puts("Unable to create window, aborting.");
        return 1;
    }

    /*Main loop*/
    while (sfRenderWindow_isOpen(window))
    {
        /*This processes the event que*/

        while (sfRenderWindow_pollEvent(window, &event))
        {
            if (event.type == sfEvtClosed)
            {
                puts("Closing render window.");
                sfRenderWindow_close(window);
            }
        }
    }
    /*Cleanup*/
    sfRenderWindow_destroy(window);
    return 0;
}
}

但如果尝试 运行 该程序,我会得到以下信息:

> Executing task: C/C++: cl.exe build active file <

Starting build...
Build finished with errors(s):
main.c
Microsoft (R) Incremental Linker Version 14.28.29333.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/debug 
"/out:C:\Users\Student\OneDrive - Liverpool Hope University\Year 1\Core 2\Guide to Assessments and Policies\Coursework-in-a-row\main.exe" 
main.obj 
main.obj : error LNK2019: unresolved external symbol __imp__sfRenderWindow_create referenced in function _main
main.obj : error LNK2019: unresolved external symbol __imp__sfRenderWindow_destroy referenced in function _main
main.obj : error LNK2019: unresolved external symbol __imp__sfRenderWindow_close referenced in function _main
main.obj : error LNK2019: unresolved external symbol __imp__sfRenderWindow_isOpen referenced in function _main
main.obj : error LNK2019: unresolved external symbol __imp__sfRenderWindow_pollEvent referenced in function _main
C:\Users\Student\OneDrive - Liverpool Hope University\Year 1\Core 2\Guide to Assessments and Policies\Coursework-in-a-row\main.exe : fatal error LNK1120: 5 unresolved externals

The terminal process terminated with exit code: -1.

Terminal will be reused by tasks, press any key to close it.

正如我所说,我是 C 的新手,所以我不太确定发生了什么,我猜测 CSFML 文件夹中的 dll 也需要以某种方式加载,但我真的不知道

因此,经过数小时的尝试、谷歌搜索、Whosebug 和 discord 服务器,我终于弄明白了...

我找到了正确的命令行参数和顺序,但一直有问题,因为它不能为 64 位编译,但可以为 x86/32 位编译

因为我使用的是 MSVC,所以你必须使用“VS 2019 的开发人员命令提示符”启动 VS 代码,事实证明它默认为 x86 构建环境,这在任何文档中都没有说明但是如果你改为打开“VS 2019 的 x64 本机工具命令提示符”并使用它打开代码,它可以使用以下 tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/I${fileDirname}/CSFML-2.5-windows-64-bit/include",
                "${fileDirname}\CSFML-2.5-windows-64-bit\lib\msvc\csfml-graphics.lib",
                "${file}",
                "/link",
                "/MACHINE:x64",
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}