سؤال

I'm trying to write an autoconf test for a C++ library. I followed http://nerdland.net/2009/07/detecting-c-libraries-with-autotools/ . My check looks like this:

SAVED_LDFLAGS=$LDFLAGS
LDFLAGS="$LDFLAGS -lMyLib"
AC_LINK_IFELSE(
  [AC_LANG_PROGRAM([#include <mylibheader.hpp>],
    [MyLibNamespace::SomeObject obj()])],
  [TEST_LIBS="$TEST_LIBS -lMyLib"] [HAVE_MYLIB=1],
  [AC_MSG_ERROR([libMyLib is not installed.])])
LDFLAGS=$SAVED_LDFLAGS

The test fails. If i check config.log the problem seems to be with the generated compilation command given by autoconf for the check:

g++ -o conftest -g -O2   -lMyLib conftest.cpp

As you can see, the -l params aren't at the end, after all inputs and outpus. If i copy&paste the conftest.cpp code from config.log i can make it compile with:

g++ -o conftest -g -O2  conftest.cpp -lMyLib 

How can autoconf be wrong? How may i fix this?

هل كانت مفيدة؟

المحلول

The problem is that you're adding -lMyLib to LDFLAGS instead of adding to LIBS. In other words, do this instead:

SAVED_LIBS=$LIBS
LIBS="-lMyLib $LIBS"
AC_LINK_IFELSE(
  [AC_LANG_PROGRAM([#include <mylibheader.hpp>],
    [MyLibNamespace::SomeObject obj()])],
  [TEST_LIBS="$TEST_LIBS -lMyLib"] [HAVE_MYLIB=1],
  [AC_MSG_ERROR([libMyLib is not installed.])])
LIBS=$SAVED_LIBS

Edit: link order of libraries is important, so I've updated the LIBS= line to link MyLib before the other libraries, if any, with the assumption that MyLib may depend on other libraries.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top