c++ccompiler-errorsglfwglew

Issues with GLFW and GLEW


As I've been learning C and C++, I started with SDL2 to visualize the programs I'm making. I use Visual Studio Code as I like the look of it and I don't want to use an IDE, and I use either the command line (gcc/g++) or the tasks.json (which by extension is just the command line I guess) to compile. I just started getting into GLFW and by extension using GLEW so I can get into some shader stuff.

My issue is twofold. I cannot static link GLFW or GLEW (it throws errors when I try to link the libraries) and even if I use the .dll files instead, some functions are not available (such as glGetString(), I haven't tried any more than that).

Here's my tasks.json file

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
            "args": [
                "-g",
                "${workspaceFolder}\\src\\**",
                "-I",
                "C:\\GLFW\\include",
                "-I",
                "C:\\GLEW\\include",
                "-L",
                "C:\\GLFW\\lib-static-ucrt",
                "-L",
                "C:\\GLEW\\lib\\Release\\x64",
                "-o",
                "${workspaceFolder}\\bin\\GLFWtest.exe",
                "-D",
                "GLEW_STATIC",
                "-lglew32s",
                "-lglfw3dll",
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C:\\msys64\\ucrt64\\bin\\g++.exe"
        }
    ]
}

I've selected the glew32s library as I want to static link but like I said it's throwing these errors

Warning: corrupt .drectve at end of def file
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0xd): undefined reference to `__imp_wglGetProcAddress'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0xf): undefined reference to `__imp_wglGetProcAddress'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0x29): undefined reference to `__imp_wglGetProcAddress'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0x48): undefined reference to `__imp_wglGetProcAddress'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0x67): undefined reference to `__imp_wglGetProcAddress'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0xf): more undefined references to `__imp_wglGetProcAddress' follow
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0xe): undefined reference to `__imp_glGetString'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0x2de): undefined reference to `__imp_glGetIntegerv'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0x2f3): undefined reference to `__imp_glGetIntegerv'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0x300): undefined reference to `__imp_wglGetProcAddress'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0x363): undefined reference to `__imp_glGetString'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0x2d): undefined reference to `__imp_wglGetCurrentDC'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0x1c): undefined reference to `__imp_wglGetProcAddress'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0x30): undefined reference to `__imp_wglGetProcAddress'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\GLEW\lib\Release\x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0x5c): undefined reference to `__imp_wglGetCurrentDC'
collect2.exe: error: ld returned 1 exit status

If I instead dynamically link, I can get it to JUST compile, but when I try to use the function I mentioned previously, I get this error

C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\jpwol\AppData\Local\Temp\ccasYrmw.o: in function `main':
C:/Coding Practice/C++/GLFWtest/src/main.cpp:16:(.text+0x101): undefined reference to `glGetString'
collect2.exe: error: ld returned 1 exit status

It should be mentioned that I am running the 64 bit compiler, and here's the entire code for the program I'm trying to compile

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


int main(void)
{

    glfwInit();
    GLFWwindow* window = glfwCreateWindow(1280, 720, "window", NULL, NULL);
    glfwMakeContextCurrent(window);
    if(glewInit() != GLEW_OK);
    {
        printf("Error!\n");
    }
    std::cout << glGetString(GL_VERSION) << '\n';
    glfwSetWindowShouldClose(window, false);
    fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
    
    return 0;
}

for some reason, it'll print "Error!" as seen on line 14 no matter what, but glewGetString(GLEW_VERSION) works fine anyways.

I've seen people say that you need to download the source for GLEW and compile it yourself, and I've tried using arguments that other people have posted but it doesn't seem to work on my system. Any help would be appreciated as compiling source from things I didn't make is probably currently out of my league.


Solution

  • All the missing symbols are in opengl32.lib which you did not link in. Add -lopengl32 to your compiler commandline.