shiona
shiona

Reputation: 559

Cross-compiling OpenGL / glew on linux for windows

I'm trying to cross-compile a small test opengl/glew program and I get linker errors from undefined references.

$ /usr/bin/i486-mingw32-g++ -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib/ -lglfw -lglew32 -lopengl32 main.cc
/tmp/cct8OpVh.o:main.cc:(.text+0x50): undefined reference to `glfwInit'
/tmp/cct8OpVh.o:main.cc:(.text+0xa6): undefined reference to `glfwOpenWindowHint'
...

The same code does work when compiling for linux:

$ g++ -I/usr/include -L/usr/lib/ -lglfw -lGLEW -lGL main.cc

One thing that caught my eye is that every exported symbol from cross-compiled libraries has an extra underscore prefix:

$ nm /usr/lib/libglfw.a | grep glfwInit$
00000000 T glfwInit

$ /usr/i486-mingw32/bin/nm /usr/i486-mingw32/lib/libglfw.a | grep glfwInit$
00000000 T _glfwInit

This seems to be a common thing since even libstdc++.a shares this property, but why is my cross-compiler linker then looking for non-underscore symbols?

Running arch with following packages (local means AUR):

community/mingw32-binutils 2.23.1-3
community/mingw32-gcc 4.7.2-1
local/mingw32-glew 1.9.0-1
local/mingw32-glfw 2.7.7-1
community/mingw32-pthreads 2.9.1-1
community/mingw32-runtime 3.20-4
community/mingw32-w32api 3.17-1

EDIT

After playing out with both pkg-config and watching glfw recompile and test itself, I came up with the following magic that seems to work, at least I'm compiling:

 /usr/bin/i486-mingw32-g++ -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib -mwindows main.cc -lglew32 /usr/i486-mingw32/lib/libglfw.a /usr/i486-mingw32/lib/libopengl32.a -static-libgcc

There are few questions though:

Upvotes: 4

Views: 3761

Answers (2)

shiona
shiona

Reputation: 559

I was able to solve my problem and, in case someone ever runs into similar situation hope this helps you.

  • There are two versions of glew32 -library in my system, glew32.a and glew32.dll.a.
    • glew32.a does not allow for using --static, glew32.dll.a does.

The two commands which compile succesfully, only first of which I've run are:

/usr/bin/i486-mingw32-g++ -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib main.cc -lglew32.dll -lglfw -lopengl32 --static
/usr/bin/i486-mingw32-g++ -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib main.cc -lglew32 -lglfw -lopengl32

Looking at my old compiling attempts, the problem was wrong order of libraries and that main.cc was after the libraries.

Upvotes: 3

Amadeus
Amadeus

Reputation: 10685

There is a program, called pkg-config that helps you to configure your compiler. See the manual pages for usage information, but, for this case, its output is:

-mwin32 -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib -lglfw -lglu32 -lopengl32 -lm -s -mwindows -e _mainCRTStartup

Try to compile with this, I guess it will work.

Upvotes: 2

Related Questions