Converting an OpenGL project to use Autotools

In a science fiction game I play, we often find ourselves asking “which side of the Sun is Mars from the Earth right now?” Since this is for a game, the exact distance is not important, just “same side” or “90 degrees ahead” is a sufficient answer. But “right now” actually means a couple hundred years in the future.

Perfect problem to write a little code to solve. Here’s what it looks like.

My team is using Conan for package management of our native code. I want to turn this Orbits project into a Conan package.

We’re using Autotools for other things, so I’l start by creating a new Autotool based project.

conan new orbits/0.1 --template autotools_exe
File saved: Makefile.am
File saved: conanfile.py
File saved: configure.ac
File saved: src/Makefile.am
File saved: src/main.cpp
File saved: src/orbits.cpp
File saved: src/orbits.h
File saved: test_package/conanfile.py

Aaaaaand….I see it is opinionated. I am going to move my code into this structure. Specifically, most of the code will got into orbits.cpp, but I do have stuff that needs to go in to main. Time to restructure.

To start, I move my existing orbit.cpp into src, over writing the orbits.cpp file in there. I rename the main function to orbits, and add in the argc argv parameters so I can call them from main. That should be enough to get it to compile, assuming I can get the Autotools configuration correct. I start by installing make sure.

ayoung@ayoung-Latitude-7420:~/devel/git/admiyo/orbits$ autoconf 
configure.ac:3: error: possibly undefined macro: AM_INIT_AUTOMAKE
...

And many other lines of errors, mostly about missing files. This gives me a way forward.

Many of the files are project administration, such as the AUTHORS file, that credits the people that work on it, or the Licenses file which, while boiler-plate, makes it unambiguous what the terms are for redistribution of code and binaries. It is tempting to just use touch to create them, but you are better off putting in the effort to at least start the files. More info here.

Remember that the README.md file will be displayed on the git repo’s landing page for the major git projects, and it is worth treating that as your primary portal for communicating with any potential users and contributors.

Autotools assumes that you have just the minimal checked in to git, and the rest of the code files are generated. I ended up with this short script to run it in order:

#!/bin/sh
 
autoreconf --install
./configure
automake
make

Since you are generating a slew of files, it is worth noting the command to remove all but the git-generated files.

First, make sure you have everything you are actively working on committed! If not, you will lose files.

Then run:

git clean -xdf .

The primary file I needed to work on was configure.ac. This is the file that generates the configure script. This script is used to test if the required dependencies are available on the system. Here’s my configure.ac

AC_INIT([orbits], [0.1], [])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AC_PROG_CXX
AM_PROG_AR
LT_INIT
AC_CHECK_LIB([glut], [glutInit])
AC_CHECK_LIB([GL],[glVertex2f])
PKG_CHECK_MODULES([GL], [gl])
AC_CONFIG_FILES([Makefile src/Makefile])
AC_OUTPUT

This was the hardest part to get right.

AC_CHECK_LIB([GL],[glVertex2f])
PKG_CHECK_MODULES([GL], [gl])

The top line checks that the libGL shared library is installed on the system, and has the glVertex2f symbol defined in it. You would think that you just need the second line, which checks for the module. It turns out that configure is also responsible for generating the linker flags in the make file. So, while the PKG_CHECK_MODULES will tell you that you have OpenGL installed, the AC_CHECK_LIB line will make you use it.

I’m sure there is more wisdom to be gained in working with Autotools, but this is enough to get me started.

Now that I have the Autotools part working (maybe even correctly?!) I need to work on making it build via conan.

But that is another story.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.