views:

1349

answers:

4

Let's consider the following program and try to compile it under Cygwin:

#include <GL/glut.h>
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glLoadIdentity();
}

It compiles and runs just fine. -I/usr/include/opengl seems to be terribly important.

g++ -I/usr/include/opengl -I../include/cygwin -L../lib/cygwin test.cpp -o test.exe -lglut32 -lglu32 -lglew32 -lopengl32

Now,

#include <GL/glew.h> // from newest NVIDIA SDK
#include <GL/glut.h>
int main(int argc, char** argv)
{
    glewInit();
    glutInit(&argc, argv);
    glLoadIdentity();
}

compiled by

g++ -I/usr/include/opengl -I../include/cygwin -L../lib/cygwin test.cpp -o test.exe -lglut32 -lglu32 -lglew32 -lopengl32

fails. How to build the second application?

First

There are several ways to build glew by from NVIDIA SDK: by VS, by cygwin, by cygwin with -D_WIN32. I also tried cygwin build of original glew from source.

VS build gives

/cygdrive/c/DOCUME~1/kelebron/LOCALS~1/Temp/ccErbecl.o:test.cpp:(.text+0xa8): undefined reference to `_glLoadIdentity'
collect2: ld returned 1 exit status

cygwin builds give many

../lib/cygwin/glew32.lib(glew.o):glew.c:(.text+0x38e): undefined reference to `_glXGetProcAddress'

and cygwin with -D_WIN32 does not compile at all (I was slightly motivated by this post).

Second

There seem to be two ways to link with OpenGL
with -L/lib/w32api
or with -L/usr/X11R6/lib -lX11 -lXi -lXmu

But, the -L directives do not change anything. I've got /usr/lib/w32api/libopengl32.a, but may be missing the X11 version (/usr/X11R6/lib/libGL?). What package should I include into Cygwin? I've installed all starting with libGL (not only...).

A: 

It sounds like you want to link against the native Win32 libraries instead of X11. Add -L/lib/w32api. Otherwise, you need to link against the X11 libraries (-L/usr/X11R6/lib -lX11 -lXi -lXmu) to get rid of the glX* linker errors..

Judge Maygarden
Thanks for the hints, I've edited the question.
kelebron
+3  A: 

I had a similar issue a while ago, and I found that there are two ways to compile opengl code under Cygwin.
The first links against the native win32api libraries glut32 glu32 opengl32 (also glui). Those are somewhat older:
glut v3.7.6
opengl v1.1
glui 2.11

The second way uses the X11 libraries glut glu gl. They have newer version number in their header files (Note that to use GL extensions requires >v1.2):
glut v4 (freeglut)
opengl v1.3
glext
...

So to use the first option comile with: -I/usr/include/opengl
and link against: -lglut32 -lGLU32 -lOpenGL32

To use the X11 version, compile with: -I/usr/include/ (which is included by default)
and link with: -lglut -lGLU -lGL (note the difference)
you could add the X11 headers and libraries but I guess their included by the GL ones.
-I/usr/X11R6/include
-L/usr/X11R6/lib -lXmu -lXi -lXext -lX11


In the Cygwin installer, the following packages are your choices:
opengl -- gives the win32 version of opengl, glut, glui
libglut-devel -- X11 version of glut (freeglut), this will also pick all dependent packages. (glproto, libGL libGLU, libX11, ...)
I also recommend including the Xorg-server since you will need an X server.

Hope this helps.

Amro
A: 

Did any previous suggestion work for you?

I am not clear whether you are facing issues in linking or compiling GLEW. I use GLEW via including source and have used by linking in past.

If you want to compile GLEW then make sure you pick right zip, as using *nix based code's scripts may not run correctly under windows (not sure of Cygwin).

GLEW should be first in header and linking order.

Ketan
+1  A: 

Just to make sure we're talking about the same issues, here's the message I used to get.

$ g++ -o glew_test.exe glew_test.cpp -lglut32 -lglew32
c:\Users\Fredrik\Users\Fredrik\AppAppData\Local\Temp/ccgSNdfr.o:
glew_test.cpp:(.text+0x1c): undefined reference to `__glutInitWithExit'
c:\Users\Fredrik\AppData\Local\Temp/ccgSNdfr.o:
glew_test.cpp:(.text+0x37): undefined reference to `__glutCreateWindowWithExit'
c:\Users\Fredrik\AppData\Local\Temp/ccgSNdfr.o:
glew_test.cpp:(.text+0x53): undefined reference to `__glutCreateMenuWithExit'
c:\Users\Fredrik\AppData\Local\Temp/ccgSNdfr.o:
glew_test.cpp:(.text+0x9e): undefined reference to `glutInitDisplayMode'
c:\Users\Fredrik\AppData\Local\Temp/ccgSNdfr.o:
glew_test.cpp:(.text+0xb2): undefined reference to `glutInitWindowPosition'
c:\Users\Fredrik\AppData\Local\Temp/ccgSNdfr.o:
glew_test.cpp:(.text+0xc6): undefined reference to `glutInitWindowSize'
collect2: ld returned 1 exit status

The following pages helped me with this issue.

Basically I had to define _STDCALL_SUPPORTED and _M_IX86 at the beginning of the file and include windows.h before glew.h

Then i used the flags -lglut32 -lglew32

If you want to try it out, here's the source as well:

#define _STDCALL_SUPPORTED
#define _M_IX86
#include <windows.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <cstdio>

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutCreateWindow("MM 2004-05");
    glewInit();
    if (glewIsSupported("GL_VERSION_2_0"))
        printf("Ready for OpenGL 2.0\n");
    else {
        printf("OpenGL 2.0 not supported\n");
    }
    return 0;
}

Note that the window, for some reason, is needed for the OpenGL2.0 test.

Styggentorsken