C++ 将SDL2库与cl.exe编译器一起使用

C++ 将SDL2库与cl.exe编译器一起使用,c++,visual-studio-code,cl,C++,Visual Studio Code,Cl,我目前尝试用VisualStudio代码编译一个C++文件,我尝试编辑 CYCPPPyFrase.JSON/COD>文件,在“代码> >包含Debug路径> /Cube变量和任务中添加包含文件夹。JSON文件添加参数,但我仍然不能编译,它给了我这个错误: 链接:致命错误LNK1104:无法打开“SDL2.lib” 以下是文件: c\u cpp\u properties.json { "configurations": [ { &

我目前尝试用VisualStudio代码编译一个C++文件,我尝试编辑<代码> CYCPPPyFrase.JSON/COD>文件,在“代码> >包含Debug路径> /Cube变量和<代码>任务中添加包含文件夹。JSON<代码>文件添加参数,但我仍然不能编译,它给了我这个错误:

链接:致命错误LNK1104:无法打开“SDL2.lib”

以下是文件:


c\u cpp\u properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:\\vclib\\include\\",
                "C:\\vclib\\SDL2_image-2.0.5\\include\\"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "/IC:\\vclib\\include",
                "/IC:\\vclib\\SDL2_image-2.0.5\\include",
                "${file}",
                "SDL2.lib", "SDL2main.lib", "SDL2_image.lib",
                "/link /LIBPATH:C:\\vclib\\lib\\x64;C:\\vclib\\SDL2_image-2.0.5\\lib\\x64",
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
tasks.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:\\vclib\\include\\",
                "C:\\vclib\\SDL2_image-2.0.5\\include\\"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "/IC:\\vclib\\include",
                "/IC:\\vclib\\SDL2_image-2.0.5\\include",
                "${file}",
                "SDL2.lib", "SDL2main.lib", "SDL2_image.lib",
                "/link /LIBPATH:C:\\vclib\\lib\\x64;C:\\vclib\\SDL2_image-2.0.5\\lib\\x64",
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
更新:第二个文件现在看起来像这样:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}",
                "SDL2.lib", "SDL2main.lib","SDL2_image.lib",
                "/IC:\\vclib\\include",
                "/IC:\\vclib\\SDL2_image-2.0.5\\include",
                "/link /LIBPATH:C:\\vclib\\lib\\x86", "/LIBPATH:C:\\vclib\\SDL2_image-2.0.5\\lib\\x86",
                "/SUBSYSTEM:CONSOLE"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
现在我有一个错误:

SDL2main.lib(SDL_windows_main.obj):错误LNK2019:外部符号未解析\uuu imp__CommandLineToArgvW@8在函数\u main\u getcmdline中引用


我也遇到了同样的问题,并最终使用了这里给出的一些答案:

我的c_cpp_properties.json如下所示:

{
"configurations": [
    {
        "name": "Win32",
        "includePath": [
            "${workspaceFolder}/**",
            "G:/SDL2/include/",
            "G:/SDL2/lib/x86/"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
    
        ],
        "windowsSdkVersion": "10.0.17763.0",
        "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe",
        "cStandard": "c17",
        "cppStandard": "c++17",
        "intelliSenseMode": "windows-msvc-x64"
    }
],
"version": 4
}
My tasks.json文件如下所示:

{
"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C++: cl.exe build active file",
        "command": "cl.exe",
        "args": [
            "/Zi",
            "/EHsc",
            "/nologo",
            "/Fe:",
            "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "${file}",
            "/I G:\\SDL2\\include\\",
            "/link G:\\SDL2\\lib\\x86\\SDL2main.lib G:\\SDL2\\lib\\x86\\SDL2.lib"
        ],
        "options": {
            "cwd": "${workspaceFolder}"
        },
        "problemMatcher": [
            "$msCompile"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    }
],
"version": "2.0.0"
}
直到我记得将SDL2.dll运行时二进制文件放在工作区目录中,它才开始工作。 包括:

#define SDL_MAIN_HANDLED
#include <G:\SDL2\include\SDL.h>
#定义SDL_MAIN_
#包括

无法打开库通常意味着以下一个或多个:1。键入的路径或名称错误,2。您正在混合32位和64位或3位。库文件已损坏。我猜您正在构建一个32位应用程序,但试图链接64位库。请注意
c\u cpp\u properties.json
对compilation@drescherjm2.你说得对,我使用的是64位库和32位应用程序。我更新了我的问题。
c\u cpp\u properties.json
仅控制intellisense,任务中的命令行。json是编译的目的