Glfw linking in vs code error

I am trying to setup a project in vs code with visual c++ but im getting these errors
main.obj : error LNK2019: unresolved external symbol __imp__glClear@4 referenced in function _main
main.obj : error LNK2019: unresolved external symbol _glfwInit referenced in function _main
main.obj : error LNK2019: unresolved external symbol _glfwTerminate referenced in function _main
main.obj : error LNK2019: unresolved external symbol _glfwCreateWindow referenced in function _main
main.obj : error LNK2019: unresolved external symbol _glfwWindowShouldClose referenced in function _main
main.obj : error LNK2019: unresolved external symbol _glfwPollEvents referenced in function _main
main.obj : error LNK2019: unresolved external symbol _glfwMakeContextCurrent referenced in function _main
main.obj : error LNK2019: unresolved external symbol _glfwSwapBuffers referenced in function _main
I am using the example code for creating a window this is my tasks.json file
{

"version": "2.0.0",

"tasks": [

    {

        "type": "cppbuild",

        "label": "C/C++: cl.exe build active file",

        "command": "cl.exe",

        "args": [

            "/EHsc",

            "-LC:\\Users\\matsw\\Desktop\\cpp\\lib",

            "-Lglfw3.lib",

            "-Lopengl32.lib",

            // obj files path

            "/Fo${workspaceFolder}\\bin\\",

            // exe file path

            "/Fe:", "${workspaceFolder}\\bin\\",

            "${file}"

        ],

        "options": {

            "cwd": "${workspaceFolder}"

        },

        "problemMatcher": [

            "$msCompile"

        ],

        "group": {

            "kind": "build",

            "isDefault": true

        },

        "detail": "compiler: cl.exe"

    }

]

}
I would appreciate if anyone could help me with this.

I assume this is with Visual Studio Code? I’m not familiar with it, but it seems you’re using cl.exe to compile - for cl.exe you don’t use -L in -Lglfw32.lib or -Lopeng32.lib to specify libraries. Instead simply pass regular filename glfw3.lib and opengl32.lib. As alternative you can specify libraries in your source code:

#pragma comment (lib, "glfw3.lib")
#pragma comment (lib, "opengl32.lib")

Yes it is with Visual Studio Code and compiling with cl.exe i tried both methods and it removes the errors but replaces it with: LINK : fatal error LNK1104: cannot open file ‘glfw3.lib’
opengl32.lib works i think.

That means you have glfw3.lib file in different folder. You need to provide path to it with -L argument:
-Lpath/to/folder

I copied the path to the folder from my file explorer and when i type in my cmd: cd C:\Users\matsw\Desktop\cpp\lib\ it goes to the folder where the glfw3.lib file is, and this gives that error.

Oops, sorry my mistake -Lpath is for gcc based compiler. For cl.exe simple pass full path to glfw3.lib, so like C:\Users\matsw\Desktop\cpp\lib\glfw3.lib or do relative path to project folder.

The docs on Visual Studio compiler are available at:

For link options (but oddly not the files to link as cl passes these on), you need to pass these after /link:

So you need to have the last argument be:

" /link /LIBPATH: “C:\Users\matsw\Desktop\cpp\lib” "

Thus args would likely be (I hope I get my vscode syntax correct here:

   "args": [

        "/EHsc",

        "glfw3.lib",

        "opengl32.lib",

        // obj files path

        "/Fo${workspaceFolder}\\bin\\",

        // exe file path

        "/Fe:", "${workspaceFolder}\\bin\\",

        "${file}",

        " /link /LIBPATH:\"C:\\Users\\matsw\\Desktop\\cpp\\lib\" "
    ],

Note that alternatively you could use vscode with cmake, and use something like:

I tried both solutions but it still gives the same error that it cannot open input file glfw3.lib

Neither @mmozeiko nor I are that familiar with vscode. If you could copy and paste the text of the compiler command issued (which I think is shown in the terminal display at the bottom) I can debug the arguments used.

Could you also check that you have the library path correctly set up, and that the glfw library you are using is 32bit (cl.exe by default compiles in 32bit mode, but I don’t know if vscode is set up to initialize the environment variables for 64bit compilation).

Finally, as suggested you could try using the cmake project with vscode as a simple way to get started. You will need the cmake extension:

/out:C:\Users\matsw\Desktop\cpp\bin\main.exe
glfw3.lib
opengl32.lib
C:\Users\matsw\Desktop\cpp\bin\main.obj
" /link /LIBPATH:C:\Users\matsw\Desktop\cpp\lib "
LINK : fatal error LNK1181: cannot open input file ‘glfw3.lib’

Build finished with error(s).
The terminal process failed to launch (exit code: -1).
I have tried both 32 and 64 just to make sure
Im also looking how cmake works like u suggested.

This doesn’t appear to be a full compiler command - indeed /out is not a MSVC compiler option, and the /link /LIBPATH option is in quotes (the path C:\Users\matsw\Desktop\cpp\lib can be but in your case doesn’t need to be since you don’t have any spaces in it).

I will try and set up vscode at some point to see what the options need to be. For the moment if you are inexperienced with MSCV and vscode I once again suggest you try the cmake starter.

Thanks for the help will try the cmake starter.