Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up GLEW and GLFW in VS Code with Visual C++

I have a basic Visual C++ environment in Visual Studio Code. I've recently started to read about OpenGL and have been trying to set up GLEW and GLFW in VS Code. Almost all of the tutorials I find fall into one of the 2 categories:

  1. GLEW and GLFW setup with MSVC, but in Visual Studio.
  2. GLEW and GLFW setup in VS Code, but with minGW.

I've read the documentation for MSVC command line and its options a few times and can't figure it out. I'm not familiar with Cmake and such, so I've been trying to do it via VS Code tasks.json file.

Directory structure

My tasks.json file is as follows:

{
    "version": "2.0.0",
    "tasks": [

        // BUILD TASK
        {
            "type": "shell",
            "label": "C/C++: build project",
            "command": "cl.exe",
            "args": [
                // exception handling args
                "/EHsc",
                // including additional headers
                "/I${workspaceFolder}\\include\\glew\\include",
                "/I${workspaceFolder}\\include\\glfw\\include",
                // obj files path
                "/Fo${workspaceFolder}\\bin\\",
                // exe file path
                "/Fe:", "${workspaceFolder}\\bin\\program.exe", 
                // source files
                "${workspaceFolder}\\src\\*.cpp",
                // library files
                "opengl32.lib",
                "${workspaceFolder}\\include\\glew\\lib\\Release\\x64\\glew32s.lib",
                "${workspaceFolder}\\include\\glfw\\lib-vc2019\\glfw3.lib"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },

        // CLEAN TASK
        {
            "type": "shell",
            "label": "C/C++: clean project",
            "command": "del",
            "args": [
                "${workspaceFolder}\\bin\\*",
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            }
        }

    ]
}

And my main.cpp file has a few lines of code to open a window.

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

using namespace std;

int main()
{
    if (!glfwInit())
    {
        cout << "Failed to initialize GLFW" << endl;
        return -1;
    }

    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window;
    window = glfwCreateWindow(1000, 700, "Test window 01", NULL, NULL);
    if (window == NULL)
    {
        cout << "Failed to open GLFW window" << endl;
        return -1;
    }
    glfwMakeContextCurrent(window);

    glewExperimental = true;
    if(glewInit() != GLEW_OK)
    {
        cout << "Failed to initialize GLEW" << endl;
        return -1;
    }
}

Full terminal output while running build task at this pastebin

like image 844
shamilsdq Avatar asked Oct 24 '25 18:10

shamilsdq


2 Answers

I had a similar issue recently and solved it by using the dlls, instead of static linking

For this, you'd add glfw3dll.lib instead of glfw3.lib (which you should have in the same zip you've downloaded from glfw website, alongside the dll) and place the dll nearby the executable.

You should also define GLFW_DLL before including any other glfw headers, as the documentation states


Update:

I was able to get static linking working. Turns out I need to link against a few system libraries. Here's the batch file I use:

REM https://stackoverflow.com/a/29379089/4894526
set SystemLibs=opengl32.lib user32.lib gdi32.lib shell32.lib 

REM https://stackoverflow.com/a/78289129/4894526
set Libs=glew32s.lib glfw3_mt.lib 

cl %SystemLibs% %Libs% main.c 
like image 112
atil Avatar answered Oct 26 '25 08:10

atil


Right I am using MinGW as the compiler and after many days trying to fix this problem my self I found that this works.

Image

For the task file you need to put (in the args in the task json for vs code):

"-g",
"-std=c++17",
"${file}",
"-I",
"./include",
"-L",
"./lib",
"-lopengl32",
"-lglew32",
"-lglfw3dll",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe" 

Then it should work. Side note you also need glfw3.dll with glew32.dll

like image 39
Electric fox Avatar answered Oct 26 '25 08:10

Electric fox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!