2016-12-09 49 views
0

我正在嘗試測試C++庫,並且必須比AC_SEARCH_LIBS或AC_CHECK_LIB做得多一點。但是,我的鏈接器對選項的順序很挑剔(g ++ version 5.4.0)。AC_LANG_PROGRAM由於鏈接器選項的順序而失敗鏈接器階段

我configure.ac包含以下代碼:

AC_LINK_IFELSE(
     [AC_LANG_PROGRAM([#include <api/BamReader.h>], [BamTools::BamReader dummy])], 
     [TEST_LIBS=="$TEST_LIBS -lbamtools"] [HAVE_BAMTOOLS=1], 
     [AC_MSG_WARN([libbamtools is not installed])]) 

我知道Bamtools被安裝在我的系統。這會產生一個負面結果:

checking api/BamReader.h usability... yes 
checking api/BamReader.h presence... no 
configure: WARNING: api/BamReader.h: accepted by the compiler, rejected by the preprocessor! 
configure: WARNING: api/BamReader.h: proceeding with the compiler's result 
checking for api/BamReader.h... yes 
configure: WARNING: libbamtools is not installed <-- this line 

經過一番調查後,它似乎是鏈接器選項的順序。

的conftest.cpp文件如下所示:

#include <api/BamReader.h> 
int main() { 
    BamTools::BamReader dummy; 
    return 0; 
} 

autoconf的宏調用

g++ -o conftest -g -O2 -I/usr/local/include/bamtools -L/usr/local/lib/bamtools -lbamtools conftest.cpp/tmp/ccZiV1J9.o: In function `main': 
/home/kzhou/coding/tmp/conftest.cpp:24: undefined reference to `BamTools::BamReader::BamReader()' 
/home/kzhou/coding/tmp/conftest.cpp:24: undefined reference to `BamTools::BamReader::~BamReader()' 
collect2: error: ld returned 1 exit status 

如果喲通過將-lbamtools到底切換順序,則鏈接很高興:

g++ -o conftest -g -O2 -I/usr/local/include/bamtools -L/usr/local/lib/bamtools conftest.cpp -lbamtools 

我想知道AC_LANG_PROGRAM是否需要更新?請給出意見。到目前爲止,我還沒有找到解決這個問題的好方法。 請參考:

https://nerdland.net/2009/07/detecting-c-libraries-with-autotools/

+1

正如Diego指出的那樣,LDFLAGS將放在對象或源代碼的前面,LIBS將放在後面。我主要的錯誤是使用這兩個變量。 –

回答

2

這就像你正在過圖書館在錯誤的變量;如果你通過LIBS庫,它會處於正確的位置,因爲autoconf做的是正確的事情。現在

,則粘貼的代碼有太多(使用==這是一個比較代替=其是分配)了語法錯誤,並且由於TEST_LIBS邏輯之一是由所述特定交你參考使用的變量。所以這不是什麼設置-lbamtools在任何訂單。

save_LIBS=$LIBS 
LIBS="$LIBS -lbamtools" 
AC_LINK_IFELSE(
     [AC_LANG_PROGRAM([#include <api/BamReader.h>], [BamTools::BamReader dummy])], 
     [save_LIBS="$LIBS"; HAVE_BAMTOOLS=1], 
     [AC_MSG_WARN([libbamtools is not installed])]) 
LIBS=$save_LIBS 

這應該做你要找的東西,雖然它比它可能要複雜一點。您可以使用AC_CHECK_TYPE來檢查是否定義了BamTools::BamReader

+0

迭戈,謝謝。學習autotools非常困難;沒有好書,而且文件非常有限。而且,沒有那麼多人掌握了autotools。我必須學習點點滴滴。 –

+0

有John Calcote的Autotools和我的Autotools Mythbuster,但後者只針對已知問題進行了指出,而不是完全解釋。 –

+0

我已閱讀John Calcote的書。它非常好,讓我瞭解整個系統。本書的第二版應該添加一些C++示例。 –

相關問題