Alessandro
Alessandro

Reputation: 21

Using SDL2 libraries with cl.exe compiler

I'm currently trying to compile a C++ file using Visual Studio Code and I tried editing the c_cpp_properties.json file adding the include folders in the includePath variable and the tasks.json file adding arguments but I still can't compile, it gives me this error:

LINK : fatal error LNK1104: can't open 'SDL2.lib'

Here are the files:

c_cpp_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
}

tasks.json

{
    "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
            }
        }
    ]
}

UPDATE: the second file looks like this now:

{
    "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
            }
        }
    ]
}

Now I have this error:

SDL2main.lib(SDL_windows_main.obj) : error LNK2019: extern symbol not resolved __imp__CommandLineToArgvW@8 referenced in function _main_getcmdline

Upvotes: 2

Views: 1333

Answers (1)

willy_64
willy_64

Reputation: 11

I had the same issue and got it to work eventually using some of the answers given here: Compile a C / SDL program with Visual C++ 2013 from the Command Line

My c_cpp_properties.json looks like:

{
"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 file looks like this:

{
"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"
}

It didn't work for a while until I remembered to put the SDL2.dll runtime binary in the workspace directory. And including:

#define SDL_MAIN_HANDLED
#include <G:\SDL2\include\SDL.h>

Which I got from Do I need SDL's main() function?

Upvotes: 1

Related Questions