C++/OpenGL Compiling Issues

Associate
Joined
14 Jun 2010
Posts
737
Afternoon,

Unfortunately, I am having some problems with C++ and OpenGL that I have been trying to sort for the last few hours. I have to book "Beginning OpenGL Game Programming Second Edition" and I have completed a couple of exercises from it and they have compiled fine with G++ on Arch Linux.

However, I am now trying to compile a main.cpp file which includes headers called glxwindow.h and example.h. Each of the header files has its own .cpp file within which are the functions (Which have been prototyped in the headers). I am getting an error when trying to access functions within the classes defined in example.h and glxwindow.h.

Code:
// I have excluded the rest of the main.cpp file for brevity
Example example; // Creating an instance of the class
example.init();

This raises an error upon compiling with G++:
Code:
 main.cpp:(.text+0xb2): undefined reference to `Example::init()'

I stripped out all of the code from the main.cpp aside from the including of the header files and the main function, which instantiates a class and then calls a function from it- still no luck.

So I am stuck as to where to go from here, main.cpp includes header files, each with their own .cpp file, and tries to access functions from them, but it doesn't work.

Any help would be great. :)
 
How are you compiling the code? The above error isn't actually from the compilation, it's a linker error that it can't find the init() function. Can you post the code for that? My C++ is a bit rusty, but I'll take a look.
 
You need to link main.cpp with example.cpp. The simplest way to do this is a one-liner with gcc:

g++ main.cpp example.cpp -lgl <more stuff here i'd imagine>

This compiles both cpp files then links them together. Otherwise you can generate .o files for each cpp file (notice the -c flag), then link them together with ld, something like this:

g++ main.cpp -o main.o -c
g++ example.cpp -o example.o -c
ld main.o example.o -lgl
 
Thank you for both of your replies, I tried running G++ as Andrew said and I got another error:

Code:
harvey ~/NetBeansProjects/Beginning OpenGL Chapter Two $ g++ main.cpp example.cpp glxwindow.cpp -lglut -lGL -lGLU -lX11
/usr/bin/ld: /tmp/ccQGjuhc.o: undefined reference to symbol 'XF86VidModeGetAllModeLines'
/usr/bin/ld: note: 'XF86VidModeGetAllModeLines' is defined in DSO /usr/lib/libXxf86vm.so.1 so try adding it to the linker command line
/usr/lib/libXxf86vm.so.1: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status

Trying to run it with the extra include generates another error:

Code:
harvey ~/NetBeansProjects/Beginning OpenGL Chapter Two $ g++ main.cpp example.cpp glxwindow.cpp -lglut -lGL -lGLU -lX11 -llibXxf86vm
/usr/bin/ld: cannot find -llibXxf86vm
collect2: error: ld returned 1 exit status
 
Not a unix person but -l in gcc auto prepends lib onto the front of the args so -llibXxf86vm should be -lXxf86vm. If it still can't find it you should be able to specify the full path.
 
Thank you very much for all your help, Animatronic you were right about it, I replaced the term lib with -l and it compiled.

Once again, thank you everyone for you help. :)
 
Back
Top Bottom