# Autoconf Tutorial Part-3

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.

Next, in your configure.ac add the following line:

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.

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.

## Wrapping it up

The full working example can be downloaded from here. Here is the listing of each code:

The default rule for gl_example_SOURCES is to look at the c program with the same name, thus can be omitted.

Try out the configure options by invoking ./configure --help. You’ll find it provides rich options that is familiar to power users.

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.