anotherCode245
anotherCode245

Reputation: 703

How to use a library only if it was enabled in configure script

I want to use a library in my code if and only if it was enabled in the configure script. The user will compile the program by first running the configure script and then make

./configure --enable-mylib
make

And I need that this --enable-mylib becomes a boolean and a define in my code so that I can use it as for example

#ifdef MYLIB
#include "mylib.h"
#endif
...
if(mylib_enabled()) {
  do_mylib_call();
}
...

What is the standard way of doing so ?

Upvotes: 0

Views: 85

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409206

For your check for the --enable-mylib you should check for the library, and if found you will have an HAVE_MYLIB macro that you can check for.

Something like this in your configure script:

AC_ARG_ENABLE([mylib],
  [  --enable-mylib          enable the special "mylib" library],
  [if test $enableval = yes; then
     AC_CHECK_LIB(mylib, function_in_mylib)
     AC_CHECK_HEADER(mylib.h)
   fi])

And this in your source file

#if HAVE_MYLIB_H
# include <mylib.h>
#endif

...

#if HAVE_MYLIB
function_in_mylib();
#endif

Upvotes: 3

Related Questions