Autoconf: set compiler flags AC_CHECK_LIB
I am trying to set up my project to use the autotools build system for distribution. However, I am having a problem when it comes to including several additional libraries.
My code depends on several libraries:
-
One I wrote (I will call libfoo)
-
Someone who is third party (I will call libbar)
-
Eigen3 (these are header files only)
They are all already compiled and installed in my $ HOME directory. I don't need to create them as part of this project.
I need flags
-std=c++11 (Got this taken care of)
and
-I/path/to/include/libfoo (or libbar, or Eigen3)
and
-L/path/to/lib/ (Where libfoo and libbar reside)
and
-lfoo
to compile and link to libfoo and libbar
The problem is I want to set up checks for the existence of libfoo and libbar and their headers (I already have a check for Eigen3). The problem is that macros like AC_CHECK_HEADERS and AC_CHECK_LIB don't seem to want to include the required CPPFLAGS or LDFLAGS no matter how I try to define them, so the configuration step doesn't work.
AC_LANG_PUSH([C++])
AC_CHECK_HEADERS([libfoo/foo.h], , [echo "Did not find libfoo headers"; exit -1])
AC_LANG_POP([C++])
Reading the config.log shows that configure is trying to compile the header with
g++ -std=c++11 (other flags...)
but failing due to missing flags above (error: "Unable to find Eigen3 / dense" or similar by specifying missing flag -I)
I'm new to autotools so I might be missing something obvious, but I've read a lot of help and tutorials and still haven't been able to figure out what I need to do to set this up.
Related: I want the user to be able to manually specify the location of libfoo and libbar via --with-libfoo-include (etc.) if needed, so I also need to tell where configure defines those libraries. How can I do that?
source to share
For anyone else who stumbled upon this wondering the same thing, I found my answer by looking at the autotools files from another open source project:
First of all, to add the -with-x-include = flag, you use
AC_ARG_WITH
Second, the crude procedure for this check involved first checking if the header file exists, even though you said it should use
AC_CHECK_FILE
then I keep the old CPPFLAGS and then add the required includes. Then I could check the title using
AC_CHECK_HEADER
and finally, using the on-success argument for this macro, I set a variable that I later test. If true, I define the LIBFOO_INC variable using the specified include path.
Finally, I also found the AX_CXX_CHECK_LIB macro, which allows you to check a library in C ++ libraries. It is available in ac-archive
As a side note, in my case I needed to define some of these flags for both CPPFLAGS and CXXFLAGS, so this has to do with manually configuring them in some places. This may not always affect people, but this is a potential opportunity.
source to share
All you have to do is:
For
-std = C ++ 11 (Takes care of this)
it is useful to use ax_cxx_compile_stdcxx_11.m4
To use it you need to download it from the above link and install it in $ (project_top) / m4 /
Then you write like below in configure.ac :
AX_CXX_COMPILE_STDCXX_11([noext], [mandatory])
And exec, you can check if it is possible or not to use the C ++ 11 feature on the platform
$ autoreconf -vi -I m4 $ ./configure
and
-I / path / to / include / libfoo (or libbar or Eigen3)
-L / path / to / lib / (where libfoo and libbar are located)
-lfoo
It's hard to explain simply
-
The library provides a .pc file
You must use the PKG_CHECK_MODULES macro You must direct the pkg-config manual and the PKG_CHECK_MODULES macro
This will help get the path to the path and library. -
The library provides a wrapper foo-config For example, libcurl provides curl-config , libxml2 provides xml2-config . So we can get the include / lib path using this wrapper.
CURL_LIBS=`$CURLCONFIG --libs` CURL_CFLAGS=`$CURLCONFIG --cflags`
-
Library written in C and providing nothing You must use AC_CHECK_HEADER / AC_CHECK_LIB
You can refer to this AC_CHECK_HEADER , AC_CHECK_LIB -
Library written in C ++ and providing nothing
Use AC_TRY_LINK / AC_LANG_PUSH , AC_LANG_POP as shown below ...AC_MSG_CHECKING([for some_cpp_lib]) AC_LANG_PUSH(C++) SAVE_LIBS="$LIBS" LIBS="$LIBS -lsome_cpp_lib" AC_TRY_LINK([#include <some_cpp_lib.hpp>], [SomeCppLib object;], has_some_cpp_lib=1, has_some_cpp_lib=0) AC_LANG_POP(C++) if test $has_some_cpp_lib = 0; then AC_MSG_RESULT([no]) else AC_MSG_RESULT([yes]) fi
source to share
I found the easiest way to get the compiler and linker flags correctly, use the PKG_CHECK_MODULES
one pkg-config
behind the scenes so that the libraries provide the correct flags.
PKG_CHECK_MODULES([FOO], [foo >= 3])
although your mileage may vary ...
source to share