2012-12-26 57 views
0

我的開源項目分發了一個Makefile。只要用戶安裝了Boost和OpenSSL,「make」本身就可以正常工作。如果不是,他會得到一個編譯錯誤。Makefile:測試頭文件的目標

我想向用戶顯示一條錯誤消息,說明如何解決問題,而不是讓他從編譯器輸出中辨別出問題。

我已經放在一起嵌入一個Makefile的小腳本,它將做一個快速和髒的編譯,以驗證是否存在先決條件頭文件之前,允許核心代碼構建。它顯示一條錯誤消息,並在代碼不能編譯時中止編譯。它似乎運作良好。

# BOOST_INCLUDE := -I/home/jselbie/boost_1_51_0 

all: myapp 

testforboost.o: 
    @echo "Testing for the presence of Boost header files..." 
    @rm -f testforboost.o 
    @echo "#include <boost/shared_ptr.hpp> " | $(CXX) $(BOOST_INCLUDE) -x c++ -c - -o testforboost.o 2>testerr; true 
    @rm -f testerr 
    @if [ -e testforboost.o ];\ 
    then \ 
     echo "Validated Boost header files are available";\ 
    else \ 
     echo "* ********************************************";\ 
     echo "* Error: Boost header files are not avaialble";\ 
     echo "* Consult the README file on how to fix";\ 
     echo "* ********************************************";\ 
     exit 1;\ 
    fi 

myapp: testforboost.o 
    $(CXX) $(BOOST_INCLUDE) myapp.cpp -o myapp 

我的腳本是一個很好的方法嗎?我假設它在Linux之外是可移植的(Solaris,BSD,MacOS)。或者還有其他的標準做法嗎?我知道Autotools可以做類似的事情,但是我對學習所有Autotools和重寫我的Makefiles並不感到興奮。

+0

當然,現在不是學習Autotools的好時機,但考慮使用CMake。 –

回答

1

原則上可以這樣。但是,因爲你只有預處理,並考慮到你可以使用任何命令的情況下,它可以簡化爲:

.PHONY: testforboost 
testforboost: 
    @echo "Testing for the presence of Boost header files..." 
    @if echo "#include <boost/shared_ptr.hpp> " | $(CXX) -x c++ -E - >/dev/null 2>&1;\ 
    then \ 
     echo "Validated Boost header files are available";\ 
    else \ 
     echo "* ********************************************";\ 
     echo "* Error: Boost header files are not avaialble";\ 
     echo "* Consult the README file on how to fix";\ 
     echo "* ********************************************";\ 
     exit 1;\ 
    fi 

OTOH,因爲你有升壓包括一個可變的路徑,爲什麼不看爲文件直接?這將需要一些字符串操作。可能很難做出來,但用makepp它會是$(map $(BOOST_INCLUDE),s/^-I//)

+0

很酷。我喜歡簡化。 BOOST_INCLUDE僅用於在其現有包含路徑中沒有的人員的手動覆蓋。我會檢查makeepp。 – selbie