0

I'm trying to compile a sample OpenGL for Windows code that uses WGL, and the compilation fails.

All the compiler errors look like: undefined reference to `wglCreateContext@4' (with different wglFunctionNames)

The source code that throws this error:

...
#include <windows.h>
#include <GL\gl.h>
#include <GL\glu.h>
...
hRC = wglCreateContext(hDC); <- this causes an error
...

I compile from the commandline using

g++ -Wall -mwindows -static-libstdc++ -lopengl32 main.cpp -o out

Accordingly to ms documentation at: https://msdn.microsoft.com/en-us/library/windows/desktop/dd374379(v=vs.85).aspx

Requirements:

Header Wingdi.h
Library Opengl32.lib
DLL Opengl32.dll

(do I need to include Wingdi.h to use wglFunctions...?) I have opengl32.dll on my system at C:\Windows\System32\opengl32.dll I have libopengl32.a at C:\MinGW\lib\libopengl32.a main.cpp is located at D:\dev\cpp\main.cpp

This is as far as my research got me, I'm stuck. Tried lots of different compiler flags, but none worked.

I know there are things like freeglut that can let me get around this issue completely, but I'd like to know how to get this to work. So, what do I need to change, to stop getting undefined references to wgl[functionName]s in my code? (wglCreateContext was just one example. every wglFunctionName throws a reference error)

Aqo
  • 343
  • 4
  • 16
  • Read in gcc manual how to tell the linker how to find the libraries. Hint: `-L` flag – Ripi2 Jan 30 '18 at 19:25
  • This question may be a duplicated of [this](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ripi2 Jan 30 '18 at 19:33
  • -L lets me add a directory and msdocs said I need Opengl32.lib, which I can find in a VS install either as x86 version or x64 version. Trying to -L"path to VS folder containing Opengl32.lib" didn't resolve the undefined references though, I don't get what I'm actually missing. – Aqo Jan 30 '18 at 20:12
  • #include in your source and try with this command: `g++ -Wall -mwindows main.cpp -o out -static-libstdc++ -lopengl32` See, in GCC the libraries must be *after* the unit (file) that uses them. – Ripi2 Jan 30 '18 at 20:39
  • I tried including wingdi.h in the source but it made no difference. also, are you sure about moving the libraries after the file that uses them? This did not solve the issue, and instead also gave me a new error: ` '-static-libstdc++' is not recognized as an internal or external command, operable program or batch file. ` – Aqo Jan 30 '18 at 20:59
  • https://stackoverflow.com/a/409470/3871028 – Ripi2 Jan 30 '18 at 21:14
  • What you are seeing is definitely a linker error. You mention that libopengl32.a is located at C:\MinGW\lib\libopengl32.a. Are you sure that your linker is using that folder when including libs? – Jeff Melton Jan 30 '18 at 21:40
  • I've added -LC:/MinGW/lib to the compiler command but the issue persists. for the record, typing -lrandomword throws an error of "cannot find -lrandomword", while -lopengl32 does not throw any error like that even without -LC:/MinGW/lib, so I assumed it does in fact find the library. – Aqo Jan 30 '18 at 21:52

0 Answers0