# Linking & Compiling GLFW with MinGW

**URL:** <https://discourse.glfw.org/t/linking-compiling-glfw-with-mingw/1969>\
**Category:** support\
**Created:** [December 27, 2021, 12:00am UTC](https://discourse.glfw.org/t/linking-compiling-glfw-with-mingw/1969 "2021-12-27T00:00:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![williamlii](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/williamlii/32/588_2.png) [@williamlii](https://discourse.glfw.org/u/williamlii)\
**Post date:** [December 27, 2021, 12:00am UTC](https://discourse.glfw.org/t/linking-compiling-glfw-with-mingw/1969/1 "2021-12-27T00:00:35Z")

</div>

Hi, I have tried for days to get GLFW to open a window in Visual Studio Code using mingw64. I have read every forum post and nothing has worked so far. I had it easily working with VS 2022 but not with VS Code.

I get an “undefined reference to `glfwInit’” error even though libglfw3.a (64 bit version) is installed and found by the compiler. I think the error has something to do with the arguments for the compiler in my tasks.json file.

I have installed mingw64 with msys64 and can successfully compile and build 64-bit c++ programs in VS Code.

I have only done what I have mentioned here, so I don’t have any magical glfw and OpenGL files floating around anywhere, besides the default opengl32.lib file that is located in the system32 windows folder.

I am using windows 10.

**File structure:**

```auto
Project
   .vscode
      tasks.json
   Dependencies
      include
         glfw3.h
         glfw3native.h
      lib
         libglfw3.a

```

**Here is my code:**

```auto
#include <iostream>
#include <GLFW/glfw3.h>

int main() {
    glfwInit();
    std::cout << "Hello World!\n";
    std::cin.get();
    return 0;
}

```

**Here are the arguments for my tasks.json file:**

```auto
"args": [
		"-fdiagnostics-color=always",
		"-g",
		"-I${workspaceFolder}/Dependencies/include",
		"-L${workspaceFolder}/Dependencies/lib",
		"-lglfw3","-lopengl32","-lgdi32", "-luser32", "-lkernel32",
		"${file}",
		"-o",
		"${fileDirname}\\${fileBasenameNoExtension}.exe"
	],

```

**Terminal Output**

```auto
Starting build...
C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g -IC:\projects\OpenGL_Test/Dependencies/include -LC:\projects\OpenGL_Test/Dependencies/lib -lglfw3 -lopengl32 -lgdi32 -luser32 -lkernel32 C:\projects\OpenGL_Test\Application.cpp -o C:\projects\OpenGL_Test\Application.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\willi\AppData\Local\Temp\ccVOsapa.o:C:\projects\OpenGL_Test/Application.cpp:5: undefined reference to `glfwInit'

collect2.exe: error: ld returned 1 exit status

Build finished with error(s).

```

---

<div class="post-metadata">

**Author:** ![somso2e](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/somso2e/32/594_2.png) [@somso2e](https://discourse.glfw.org/u/somso2e)\
**Post date:** [January 11, 2022, 12:21pm UTC](https://discourse.glfw.org/t/linking-compiling-glfw-with-mingw/1969/2 "2022-01-11T12:21:42Z")

</div>

I’m having the same issue. Were you able to solve it?

---

<div class="post-metadata">

**Author:** ![williamlii](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/williamlii/32/588_2.png) [@williamlii](https://discourse.glfw.org/u/williamlii)\
**Post date:** [January 25, 2022, 3:21pm UTC](https://discourse.glfw.org/t/linking-compiling-glfw-with-mingw/1969/3 "2022-01-25T15:21:58Z")

</div>

No, I’m still having the same problem. I have given up and I’m currently continuing my work in visual studio 2022 instead.

---

<div class="post-metadata">

**Author:** ![elmindreda](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/elmindreda/32/26_2.png) [@elmindreda](https://discourse.glfw.org/u/elmindreda)\
**Post date:** [January 26, 2022, 8:47pm UTC](https://discourse.glfw.org/t/linking-compiling-glfw-with-mingw/1969/4 "2022-01-26T20:47:06Z")

</div>

With compilers like GCC you need to list files and libraries before the files and libraries they depend on. In the command-line above your source file is listed after the GLFW library and the system libraries. Since there is nothing listed after the source file that defines `glfwInit` the link fails.

Moving the source file before the libraries should fix this problem.

---

<div class="post-metadata">

**Author:** ![somso2e](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/somso2e/32/594_2.png) [@somso2e](https://discourse.glfw.org/u/somso2e)\
**Post date:** [February 15, 2022, 1:24pm UTC](https://discourse.glfw.org/t/linking-compiling-glfw-with-mingw/1969/5 "2022-02-15T13:24:21Z")

</div>

lol. same. i gave up after 3-4days of trying and installed vs

---

<div class="post-metadata">

**Author:** ![Md\_Vicky](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@Md\_Vicky](https://discourse.glfw.org/u/Md_Vicky)\
**Post date:** [January 10, 2024, 7:45pm UTC](https://discourse.glfw.org/t/linking-compiling-glfw-with-mingw/1969/7 "2024-01-10T19:45:34Z")

</div>

Hey!  
Can you please! Provide more contexts.

---

<div class="post-metadata">

**Author:** ![Md\_Vicky](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@Md\_Vicky](https://discourse.glfw.org/u/Md_Vicky)\
**Post date:** [January 10, 2024, 7:47pm UTC](https://discourse.glfw.org/t/linking-compiling-glfw-with-mingw/1969/8 "2024-01-10T19:47:13Z")

</div>

Getting same errors. lol

---

<div class="post-metadata">

**Author:** ![Mesidex](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/mesidex/32/860_2.png) [@Mesidex](https://discourse.glfw.org/u/Mesidex)\
**Post date:** [January 14, 2024, 9:57pm UTC](https://discourse.glfw.org/t/linking-compiling-glfw-with-mingw/1969/9 "2024-01-14T21:57:19Z")

</div>

Hi,

Please ensure that your source or object files (\*.cpp, \*.c, \*.o) are positioned before the library dependencies (-L, -l). The order of the arguments passed to the compiler has an effect in this situation.

Here is how the final command that gets passed could look like:

```auto
g++ -Iinclude src/main.cpp -o build/main.exe -Llib -lglfw3 -lopengl32 -lgdi32

```

---

<div class="post-metadata">

**Author:** ![jomoly](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/jomoly/32/863_2.png) [@jomoly](https://discourse.glfw.org/u/jomoly)\
**Post date:** [January 20, 2024, 8:52am UTC](https://discourse.glfw.org/t/linking-compiling-glfw-with-mingw/1969/10 "2024-01-20T08:52:34Z")

</div>

in tasks.json  
please put the lib “-lglfw3”,“-lopengl32”,“-lgdi32”, “-luser32”, “-lkernel32”,  
behind the “${fileDirname}\${fileBasenameNoExtension}.exe”,  
or at the end of this bracelet  
hope that helps

---

<div class="post-metadata">

**Author:** ![HuanXX525](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/huanxx525/32/1004_2.png) [@HuanXX525](https://discourse.glfw.org/u/HuanXX525)\
**Post date:** [February 11, 2025, 8:24am UTC](https://discourse.glfw.org/t/linking-compiling-glfw-with-mingw/1969/11 "2025-02-11T08:24:24Z")

</div>

I have the same question, I solved this by add “-lglfw3dll”，use the file in zip from [Windows pre-compiled binaries x64](https://github.com/glfw/glfw/releases/download/3.4/glfw-3.4.bin.WIN64.zip)
