In this post I’ll show an example of how to write a cross-plaform OpenGL
program. We’ll explore more autoconf features, including config.h
, third party
libraries, and many more.
Cross plaform OpenGL
Although OpenGL API is basically the same on all platforms, their headers and
linking options are very different on different plaforms! To use OpenGL on OSX,
you must include <OpenGL/gl.h>
, however on other platform you have to use
<GL/gl.h>
. Sometimes you might have multiple possible OpenGL implementation on
the same platform. If you search for OpenGL tutorials, most of it can only built
on one platform.
And that where autoconf comes to play its role. I recently submit a new version of AX_CHECK_GL, that can address these complicated portability issues.
Check out the three OpenGL checking macros: AX_CHECK_GL, AX_CHECK_GLU, and AX_CHECK_GLUT on autoconf archive. The syntax is very easy.
AX_CHECK_GL([ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND])
AX_CHECK_GLU([ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND])
AX_CHECK_GLUT([ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND])
However, it doesn’t come with the default autoconf package, you need to include the third party autoconf archive in your build script. Here’s how to do it.
Adding Extra Macros
First, install third party macros by git submodule. Alternatively you can just copy the macros you need, but be sure to include all the dependent macro it uses.
1
|
|
Next, in your configure.ac
add the following line:
1 2 |
|
After these two steps you are free to invoke 500+ macros in the archive package.
C Preprocessor macros
Just adding the macro is not enough. You also have to pass the C preprocessor
macros to your C program. To do so, add another line to your configure.ac
.
1
|
|
And now in your C program you can write the following to make it portable on all systems. The listing is availabe in the AX_CHECK_GL document.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Wrapping it up
The full working example can be downloaded from here. Here is the listing of each code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
The default rule for gl_example_SOURCES
is to look at the c program with the
same name, thus can be omitted.
1
|
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
Try out the configure options by invoking ./configure --help
. You’ll find it
provides rich options that is familiar to power users.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
So far I haven’t seen other build system that can do OpenGL cross platform setup. (I only searched for CMake and Scons). Though autoconf is said to be harder to learn, but by learning through these three articles, now the syntax shouldn’t be that alien anymore, right?
In the next post, I’ll give another example of how to build a library, with unit tests and debugger setup.