Failing to link GLEW library

Viewed 438

this is my makefile:

### OUTPUT ###
BUILD = Build

### SOURCES ###
SRC = Src/main\
      Src/Game/Game\
      Src/Shader/Shader

### OBJECTS ###
OBJ = $(addsuffix .o, $(addprefix $(BUILD)/, $(SRC)))

### INCLUDES ###
INC = -ID:/Development/OpenGL/Glfw/include/GLFW\
      -ID:/Development/OpenGL/Glew/glew-2.1.0/include/GL\
      -ID:/Development/OpenGL/Glm/glm-0.9.9.8/glm\
      -ISrc/Game\
      -ISrc/Shader/

### LIBRARIES ###
LIB = -LD:/Development/OpenGL/Glew/glew-2.1.0/lib/Release/Win32 -lglew32s\
      -LD:/Development/OpenGL/Glfw/lib-mingw -lglfw3\
      -lopengl32

### LINKER FLAGS ###
LFLAGS = $(LIB)

### COMPILER FLAGS
CFLAGS = $(INC)

### COMPILER ###
CC = g++

all: $(BUILD)/test.exe

$(BUILD)/test.exe: $(OBJ)
    @echo LINKING $^
    @$(CC) $(LFLAGS) -o $@ $^

$(BUILD)/%.o: %.cpp
    @echo COMPILING $<
    @mkdir -p $(subst /,\,$(dir $@))
    @$(CC) $(CFLAGS) -M -MT $@ -o $(patsubst %.o, %.d, $@) $<
    @$(CC) $(CFLAGS) -o $@ -c $<

-include $(OBJ:.o=.d) 

.PHONY: clean

clean:
    @echo CLEANING......
    @@rm -rf $(BUILD)/*

i am specifying the needed libraries here:

### LIBRARIES ###
LIB = -LD:/Development/OpenGL/Glew/glew-2.1.0/lib/Release/Win32 -lglew32s\
      -LD:/Development/OpenGL/Glfw/lib-mingw -lglfw3\
      -lopengl32

and linking them here:

$(BUILD)/test.exe: $(OBJ)
    @echo LINKING $^
    @$(CC) $(LFLAGS) -o $@ $^

the errors i am getting are:

LINKING Build/Src/main.o Build/Src/Game/Game.o Build/Src/Shader/Shader.o
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0xb): undefined reference to `__glewUseProgram'
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0x33): undefined reference to `__glewCreateShader'
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0x48): undefined reference to `__glewShaderSource'
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0x70): undefined reference to `__glewCompileShader'
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0xdf): undefined reference to `__glewCreateShader'
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0xf4): undefined reference to `__glewShaderSource'
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0x11c): undefined reference to `__glewCompileShader'

also i have defined GLEW_STATIC:

#define GLEW_STATIC
#include "glew.h"

i am using mingw32 and i'm calling the makefile with mingw32-make all I have no idea why it can't find those symbols...i've been trying to figure it out for 2 hours..

EDIT: Here is the actual linking command:

g++ -LD:/Development/OpenGL/Glew/glew-2.1.0/lib/Release/Win32 -lglew32s -LD:/Development/OpenGL/Glfw/lib-mingw -lglfw3 -lopengl32 -o Build/test.exe Build/Src/main.o Build/Src/Game/Game.o Build/Src/Shader/Shader.o

1 Answers

In a single-pass linker (like almost all linkers today) the order of items is critical. In particular, you have to make sure that all your object files appear on the link line first, before any libraries. Secondly you have to make sure that libraries are ordered correctly, so that the higher level libraries come earlier and the lower level libraries (that other libraries depend on) come afterwards.

You need to extract your library flags into two distinct parts: one part that has linker control options (such as -L) and another part that contains the actual libraries themselves (such as -l).

In standard makefiles these parts are placed into the variables LDFLAGS and LDLIBS, respectively:

LDFLAGS = -LD:/Development/OpenGL/Glew/glew-2.1.0/lib/Release/Win32 \
          -LD:/Development/OpenGL/Glfw/lib-mingw

LDLIBS = -lglew32s -lglfw3 -lopengl32

I don't know enough about GLEW to know if the LDLIBS are ordered properly you may need to experiment.

Then, write your link rule like this:

$(BUILD)/test.exe: $(OBJ)
        @echo LINKING $^
        @$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
Related