Makefile:测试头文件的目标

时间:2012-12-26 07:20:04

标签: makefile

我的开源项目分发了一个Makefile。只要用户安装了Boost和OpenSSL,“make”本身就可以正常工作。如果没有,他会收到编译错误。

我想向用户显示一条错误消息,其中包含有关如何修复的说明,而不是让他从编译器输出中识别问题。

我已经将一个小脚本放在一个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并重新编写我的Makefile感到兴奋。

1 个答案:

答案 0 :(得分: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,因为你在变量中有boost包含路径,为什么不直接查找文件呢?这需要一些字符串操作。可能难以制作,但使用makepp时,$(map $(BOOST_INCLUDE),s/^-I//)

相关问题