GLFW Won't Link Correctly
Asked Answered
C

2

4

So recently I've started a project involving GLFW (64-bit, with GLEW). However, I can't seem to get it to link correctly. Here's how I'm set up:

OS: Windows 8 64-bit

Compiler: mingw64

IDE: eclipse

My simple test program:

#include <stdio.h>
#include <stdlib.h>

#define GLEW_STATIC
#include <gl/glew.h>
#include <gl/glfw3.h>

int main(void) {
    glfwInit();
puts("Hello, World!");
    return (EXIT_SUCCESS);
}

How I've set up the linking: https://i.sstatic.net/PcQiD.png

The errors (note these only occur when referencing any GLFW function. They don't occur by simply including the header):

13:33:00 **** Incremental Build of configuration Release for project MementoLibrary ****
Info: Internal Builder is used for build
gcc -O3 -Wall -c -fmessage-length=0 -o "src\\MementoLibrary.o" "..\\src\\MementoLibrary.c" 
gcc -o MementoLibrary.exe "src\\MementoLibrary.o" -lglfw3 -lglew32s -lopengl32 
c:/mingw64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/lib/../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x2a7): undefined reference to `__imp_CreateDCW'
c:/mingw64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/lib/../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x2e9): undefined reference to `__imp_GetDeviceCaps'
c:/mingw64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/lib/../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x2fb): undefined reference to `__imp_GetDeviceCaps'
c:/mingw64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/lib/../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x31e): undefined reference to `__imp_DeleteDC'
c:/mingw64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/bin/ld.exe: c:/mingw64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/lib/../lib/libglfw3.a(win32_monitor.c.obj): bad reloc address 0x0 in section `.pdata'
collect2.exe: error: ld returned 1 exit status
Cyril answered 7/9, 2013 at 17:34 Comment(3)
The fact that it is looking for __imp... implies it is trying to do DLL imports. Unless you compiled glfw yourself in MinGW (MinGW cannot link against MSVC created DLLs) chances are it will not be able to use a DLL. Do you have a static linking version of glfw?Juicy
@AndonM.Coleman: I've actually got it successfuly running using the DLL version. However, I'd prefer to have it statically linked if possible, let I can't get it working with a static compilation. Should I compile it myself?Cyril
Probably not, I just looked at the site. And the binary distribution includes static and dynamic libraries for MinGW. It should just be a matter of using the right libraries.Juicy
B
3

Your static GLFW library is compiled with the _GLFW_NO_DLOAD_WINMM compile-time macro enabled. This and other such macros may be found in the GLFW config header.

Defining this causes GLFW to assume that you will be linking against winmm (winmm.lib on Visual C++ or libwinmm.a on MinGW). This is not the default setting for the static version of the library, so I assume you have compiled it yourself. You can either add winmm to your link-time dependencies or not define _GLFW_NO_DLOAD_WINMM when compiling GLFW.

Either solution should make your program link.

Beachcomber answered 28/10, 2013 at 21:7 Comment(0)
T
0

I downloaded the GLFW library and compiled in msys2 from source. I'm not familiar enough with cmake, so I just copied the glfw-3.3.6/examples/offscreen.c to myproject/main.cpp. I also got the undefined reference to __imp_CreateDCW error (and others). I just needed to add -lgdi32 to linker flags.

Here is my compilation command:

g++ -I../glfw-3.3.6/include -I../glfw-3.3.6/deps main.cpp ../glfw-3.3.6/deps/glad_gl.c -o main -L../glfw-3.3.6/build/src -lglfw3 -lgdi32

and here is my Makefile:

GLFW_DIR = ../glfw-3.3.6
CXXFLAGS += -I$(GLFW_DIR)/include
CXXFLAGS += -I$(GLFW_DIR)/deps
LFLAGS += -L$(GLFW_DIR)/build/src -lglfw3
LFLAGS += -lgdi32

main: main.cpp $(GLFW_DIR)/deps/glad_gl.c
    $(CXX) $(CXXFLAGS) $^ -o $@ $(LFLAGS)
Tintometer answered 19/1, 2022 at 19:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.