Static Linking in GCC with VSCode not working, Giving Error: “undefined reference to glfwInit”

Issue

This Content is from Stack Overflow. Question asked by Rakesh Solanki

I am using GCC compiler with VSCode on Windows 10 for C Programming. The code is an example code from the website of GLFW. I want to static link the GLFW library so I put it with the working directory and it doesn’t give a linking error, which means the linker has found the libglfw3.a file and object file is also created in temp file, but it gave this error: undefined reference to `glfwInit’

Here is the task.json file code:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:/MinGW/bin/gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:/MinGW/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Here is the c_cpp_properties.json file:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "${vcpkgRoot}/x64-windows/include",
                "${vcpkgRoot}/x86-windows/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "8.1",
            "compilerPath": "C:/MinGW/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x86"
        }
    ],
    "version": 4
}

Here is the C program:

#include <stdio.h>
#include <conio.h>
#include "GLFW/glfw3.h"

int main(void)
{
    GLFWwindow *window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

These are the errors:

Starting build...
C:/MinGW/bin/gcc.exe -fdiagnostics-color=always -g C:UsersRAKESHVSCodeProjectCLetUsCChapter-20Example-1.c -o C:UsersRAKESHVSCodeProjectCLetUsCChapter-20Example-1.exe
C:UsersRAKESHAppDataLocalTempccgvV8hh.o: In function `main':
C:/Users/RAKESH/VSCodeProject/C/LetUsC/Chapter-20/Example-1.c:10: undefined reference to `glfwInit'
C:/Users/RAKESH/VSCodeProject/C/LetUsC/Chapter-20/Example-1.c:14: undefined reference to `glfwCreateWindow'
C:/Users/RAKESH/VSCodeProject/C/LetUsC/Chapter-20/Example-1.c:17: undefined reference to `glfwTerminate'
C:/Users/RAKESH/VSCodeProject/C/LetUsC/Chapter-20/Example-1.c:22: undefined reference to `glfwMakeContextCurrent'
C:/Users/RAKESH/VSCodeProject/C/LetUsC/Chapter-20/Example-1.c:28: undefined reference to `_imp__glClear@4'
C:/Users/RAKESH/VSCodeProject/C/LetUsC/Chapter-20/Example-1.c:31: undefined reference to `glfwSwapBuffers'
C:/Users/RAKESH/VSCodeProject/C/LetUsC/Chapter-20/Example-1.c:34: undefined reference to `glfwPollEvents'
C:/Users/RAKESH/VSCodeProject/C/LetUsC/Chapter-20/Example-1.c:25: undefined reference to `glfwWindowShouldClose'
C:/Users/RAKESH/VSCodeProject/C/LetUsC/Chapter-20/Example-1.c:37: undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status

I have no experience with linking libraries in VSCode but this error is not about linking files but rather linker is not able to find the referenced function definition and I don’t know how can I solve. Anyone please help.



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?