Undefined reference when linking with glew

Hi
I'm using mingw and im trying to link with glew but i keep getting an "undefined reference" error.

I first tried to download the windows binaries from glews website and link with that, but that gives me an undefined reference error.

I then tried linking with the static lib (glew32s.lib - instead of glew32.lib) but that too gives me the undefined reference error.

Lastly i tried downloading the source and build it myself, which i was able to do, but when i link with the generated libglew32.a i still get an undefined reference error.


My src code:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <GL/glew.h>
#include <iostream>

int main()
{
  if(glewInit() == GLEW_OK)
    std::cout << "ok\n";
  else
    std::cout << "not ok\n";

  return 0;
}


My make file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CXX = g++
CXXFLAGS = -Wall -std=c++11

INCLUDES = -I ./glew-2.1.0/include
LIBS = -L ./glew-2.1.0/lib -lglew32

OUT = ./mandel.exe

default:
	@$(CXX) $(CXXFLAGS) -DGLEW_STATIC $(INCLUDES) $(LIBS) ./*.cpp -o $(OUT)

run: default
	@$(OUT)

 */


The error:
1
2
3
4
C:\Users\stav\AppData\Local\Temp\ccnDzUU3.o:mainer.cpp:(.text+0xc): undefined reference to `glewInit@0'
collect2.exe: error: ld returned 1 exit status
makefile:10: recipe for target 'default' failed
mingw32-make: *** [default] Error 1 


Does anyone know what is going wrong?
Last edited on
Hello, I believe g++ is very particular about the order in which you include your source files.

I'll explain my steps:
1. I downloaded glew 2.1.0 and built libglew32.a (with msys2).
1. a. While trying to run make, I was previously running into this error https://stackoverflow.com/questions/40392221/undefined-reference-to-fprintf-glewinfo-c-building-glew-makefile-with-msys but the answer to that question fixed it (prevent glew from preventing linkage with stdio).
1. b. (Since it sounds like you already built the .a file, you probably don't need to worry about this)

2. Afterwards, with your same src code, I tried to build it with the following command line:
g++ -Wall -I C:/libraries/glew-2.1.0/include -L C:/libraries/glew-2.1.0/lib -lglew32 main.cpp -o out.exe

And got :
Temp\ccdZVbMB.o:main.cpp:(.text+0x10): undefined reference to `__imp_glewInit'
collect2.exe: error: ld returned 1 exit status


3. After this, I changed the ordering of the command-line options.
g++ -Wall main.cpp -I C:/libraries/glew-2.1.0/include -L C:/libraries/glew-2.1.0/lib -lglew32 -o out.exe

And it compiled.

I moved main.cpp before any linker dependencies. I believe this is what matters, because g++ only looks for existing dependencies when looking through the libraries linked (that's what I believe to be true, anyway).

According to your makefile, it looks like your command-line is being formed in such as a way that the *.cpp files are being put a the END of the command line. Move the *.cpp part to BEFORE you include your linker options.

Personally, I'd prefer using GLAD over GLEW because it only requires 1 header file and implementation file, which is auto-generated by the tool at https://github.com/Dav1dde/glad
I personally dislike GLEW's site because it's one of those sites that conflates MS Windows and Visual Studio.

Edit: One caveat, even when specifying DGLEW_STATIC, I still needed to put glew32.dll inside the same folder as out.exe. I don't know why. But whatever. It runs. Note that glewInit requires a proper window context to exist to return OK.

Last edited on
It worked!

Thank you so much (i moved the *.cpp part before my linker options as you said).
Its kind of embarrassing but i've actually run in to this problem before
but i keep forgetting about it and then when i try to create a new project a couple of months later i end up spending hours trying to figure out whats wrong.
Last edited on
Topic archived. No new replies allowed.