如何在系统无Boost时忽略boost_test目录的构建配置?
解决方案
第一步:修改configure.ac文件
首先调整Boost的检查逻辑,从强制要求改为可选检测,同时支持用户手动开关该功能:
把原来的强制检查代码:
BOOST_REQUIRE([1.48]) BOOST_TEST
替换为以下内容:
# 新增Boost测试开关,默认自动检测 AC_ARG_ENABLE([boost-tests], [AS_HELP_STRING([--enable-boost-tests], [启用Boost单元测试(默认自动检测)])], [], [enable_boost_tests=auto]) have_boost_test=no if test "$enable_boost_tests" != "no"; then AC_MSG_CHECKING([是否存在版本 >= 1.48 的Boost Test库]) boost_ok=yes # 检测Boost基础版本 BOOST_REQUIRE([1.48], [], [boost_ok=no]) # 检测Boost Test组件 BOOST_TEST([], [boost_ok=no]) if test "$boost_ok" = "yes"; then have_boost_test=yes AC_MSG_RESULT([是]) else AC_MSG_RESULT([否]) # 如果用户强制要求开启Boost测试则直接报错 if test "$enable_boost_tests" = "yes"; then AC_MSG_ERROR([--enable-boost-tests 参数要求安装 >=1.48版本的Boost Test库,但未检测到符合要求的版本]) fi fi fi # 定义条件编译标记,供Makefile调用 AM_CONDITIONAL([HAVE_BOOST_TEST], [test "$have_boost_test" = "yes"])
之后修改AC_OUTPUT配置,把boost_test/Makefile从固定输出列表中移除,改为条件生成:
把原来的AC_OUTPUT块:
AC_OUTPUT([ Makefile include/Makefile extra/Makefile comm/Makefile log/Makefile strings/Makefile app1/Makefile app2/Makefile app3/Makefile http/Makefile locks/Makefile lib/Makefile test/Makefile boost_test/Makefile ])
替换为:
# 仅检测到Boost Test时生成对应目录的Makefile if test "$have_boost_test" = "yes"; then AC_CONFIG_FILES([boost_test/Makefile]) fi AC_OUTPUT([ Makefile include/Makefile extra/Makefile comm/Makefile log/Makefile strings/Makefile app1/Makefile app2/Makefile app3/Makefile http/Makefile locks/Makefile lib/Makefile test/Makefile ])
第二步:修改顶层Makefile.am文件
调整SUBDIRS的定义,仅当检测到Boost Test时才把boost_test目录加入编译链:
把原来的SUBDIRS行:
SUBDIRS = include extra comm log strings app1 app2 app3 http locks lib test boost_test
替换为:
SUBDIRS = include extra comm log strings app1 app2 app3 http locks lib test if HAVE_BOOST_TEST SUBDIRS += boost_test endif
第三步:验证修改
修改完成后重新生成configure脚本即可:
- 执行
autoreconf -ivf更新构建配置 - 运行
./configure时如果检测到符合要求的Boost,会自动生成boost_test目录的Makefile并参与编译;未检测到则完全忽略该目录,不会影响正常构建 - 如果需要强制开启Boost测试,可执行
./configure --enable-boost-tests,此时如果未找到符合要求的Boost会直接报错终止。
boost_test目录下的原有Makefile.am无需修改,未检测到Boost时该目录不会进入构建流程。
内容的提问来源于stack exchange,提问作者hermit.crab
相关产品推荐
相关产品推荐

