Kieran Crayn
Kieran Crayn

Reputation: 27

MinGW32 linking error - builds in one command, but fails using Makefile

The issue I'm having relates to building a GLFW example. In Command Prompt if I try and build the program using one line (as below) then it works.

g++ -g -Wall -Ideps/include/ main.cpp -Ldeps/lib/ -lglfw3 -lopengl32 -lgdi32 -o test.exe

However I'm tying to build it using the following Makefile.

PROG = test.exe
CC = g++
CPPFLAGS = -g -Wall -Ideps/include/
LDFLAGS = -Ldeps/lib/ -lglfw3
LDFLAGS += -lopengl32 -lgdi32
OBJS = main.o

$(PROG) : $(OBJS)
    $(CC) $(LDFLAGS) -o $(PROG) $(OBJS)

main.o :
    $(CC) $(CPPFLAGS) -c main.cpp

But when I do so, I run into the following errors:

main.o: In function `main':
C:\Users\Kieran\GLFWtest/main.cpp:9: undefined reference to `glfwSetErrorCallback'
C:\Users\Kieran\GLFWtest/main.cpp:10: undefined reference to `glfwInit'
C:\Users\Kieran\GLFWtest/main.cpp:12: undefined reference to `glfwCreateWindow'
C:\Users\Kieran\GLFWtest/main.cpp:15: undefined reference to `glfwTerminate'
C:\Users\Kieran\GLFWtest/main.cpp:18: undefined reference to `glfwMakeContextCurrent'
C:\Users\Kieran\GLFWtest/main.cpp:19: undefined reference to `glfwSwapInterval'
C:\Users\Kieran\GLFWtest/main.cpp:20: undefined reference to `glfwSetKeyCallback'
C:\Users\Kieran\GLFWtest/main.cpp:25: undefined reference to `glfwGetFramebufferSize'
C:\Users\Kieran\GLFWtest/main.cpp:27: undefined reference to `_imp__glViewport@16'
C:\Users\Kieran\GLFWtest/main.cpp:28: undefined reference to `_imp__glClear@4'
C:\Users\Kieran\GLFWtest/main.cpp:29: undefined reference to `_imp__glMatrixMode@4'
C:\Users\Kieran\GLFWtest/main.cpp:30: undefined reference to `_imp__glLoadIdentity@0'
C:\Users\Kieran\GLFWtest/main.cpp:31: undefined reference to `_imp__glOrtho@48'
C:\Users\Kieran\GLFWtest/main.cpp:32: undefined reference to `_imp__glMatrixMode@4'
C:\Users\Kieran\GLFWtest/main.cpp:33: undefined reference to `_imp__glLoadIdentity@0'

There are a few more errors, but they are just more undefined references. I tried running the Makefile by manually calling the following two commands, one compiling and one linking. It compiles fine, but gives the same errors as above when linking,

g++ -g -Wall -Ideps/include/ -c main.cpp
g++ -Ldeps/lib/ -lglfw3 -lopengl32 -lgdi32 -o test.exe main.o

I'm at a loss as to why building it in one line works, but compiling and then linking fails when using what I believe is essentially the same process. I am relatively new to Makefiles, so it is possible I'm making some rookie error, but my searching has failed to produce any answers. I'd also like to note that building using a similar Makefile on my Mac without any problems.

Any help would be greatly appreciated, thanks.

Upvotes: 1

Views: 110

Answers (1)

HolyBlackCat
HolyBlackCat

Reputation: 96013

Try to put .o files before -l* flags.

Like this: g++ main.o -Ldeps/lib/ -lglfw3 -lopengl32 -lgdi32 -o test.exe

And modify makefile in the same way.

Upvotes: 1

Related Questions