GLFW links successfully, but I still get errors (Code Blocks)

Hello,

I’m a newbie who has had pretty bad experiences with working with external libraries. It seems every time I try to set one up, even if I follow the instructions to the letter, something goes wrong, I search Google, try a solution that supposedly is supposed to fix it, it doesn’t work, repeat, and eventually give up. GLFW is no exception.

I’m using 32-bit MinGW in Code Blocks on Windows 10 64-bit, so I downloaded the pre-compiled Win32 binaries, ensuring there are no errors caused by using incompatible binaries. I set up my paths in the linker to the installation and #included the GLFW/glfw3.h header file. At first, everything seems to work fine - the libraries have clearly been linked correctly since it knows where glfw3.h is and does not report errors in finding it. But now I go to set up a very simple program:

#include <stdio.h>
#include <stdlib.h>
#include <GLFW/glfw3.h>

using namespace std;

int main()
{
    glfwInit();
    return 0;
}

And it spits out this:

||=== Build: Debug in zztest3 (compiler: GNU GCC Compiler) ===|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `CreateDCW@16'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `GetDeviceCaps@8'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `GetDeviceCaps@8'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `DeleteDC@4'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `GetDeviceCaps@8'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `GetDeviceCaps@8'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `GetDeviceCaps@8'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `GetDeviceCaps@8'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `GetDeviceCaps@8'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| more undefined references to `GetDeviceCaps@8' follow|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `CreateDCW@16'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `GetDeviceGammaRamp@8'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `DeleteDC@4'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `CreateDCW@16'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `SetDeviceGammaRamp@8'|
C:\Users\Patrick\Desktop\PersonalProjects\glfw-3.3.1.bin.WIN32\lib-mingw\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `DeleteDC@4'|
||error: ld returned 1 exit status|
||=== Build failed: 17 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

I also tried downloading the 64-bit GLFW binaries just to see what would happen. After linking those, it gives an entirely different error: “undefined reference to glfwInit”… despite the fact that Code Blocks can find where it is located just fine. I downloaded 64-bit MinGW expecting that the problem would be fixed since I’m now using a 64-bit compiler, but I get the exact same error. So regardless of whether I use the 32-bit or 64-bit binaries, I am completely stuck.

I toiled for hours on Google trying to figure out what I’m doing wrong and none of the advice / suggestions I have ran across worked. Any help is greatly appreciated.

MinGW is a difficult environment to use for complex programs. Using the free community edition of Visual Studio on Windows can be easier.

Going back to your first error, you didn’t mention the compilation arguments you used, so if you have further issues do make sure to post those along with your error.

You might find my replies in this post useful.

Essentially you need to make sure you include all the required libaries, which can depend on the version of MinGW you are using. Something like the following should work:

g++ mycode.cpp -lglfw3 -lopengl32 -lgdi32 -luser32 -lkernel32

This is the command line example, so for Code Blocks you need to specify those libraries somewhere.

There is a little more information in this article on building applications with GLFW.