csdl-2autotools

Make Autotools project with SDL2


I am spinning up a new project. I want to use autotools for my build system, and the project will link to SDL2. However, I'm stuck writing the configure.ac file.

The documented way to link to SDL2 when it is installed on the system uses an installed command that outputs linker flags:

gcc -o myprogram myprogram.c `sdl2-config --cflags --libs`

I find it reasonable to assume that the output of sdl2-config may change depending on the system, and that it should be run in configure, then passed to make (this may be wrong!). I'm having a dog of a time trying to set this up, though. Maybe it's because my knowledge of M4sh is very limited, but I can't figure out how to do command substitution on the target computer.

How do I properly integrate linking SDL2 and autotools into a project, and why is it done that way so I can apply those principles to future libraries I have this issue with?


Solution

  • I agree with @Orion's answer that using PKG_CHECK_MODULES is the way to do this. I have a few remarks to add.

    This simplifies the configure.ac snippet to:

    m4_pattern_forbid([^PKG_CHECK_MODULES])dnl
    PKG_CHECK_MODULES([SDL2], [sdl2 >= 2.0.22])
    

    About the Makefile.am snippet: