configure.ac和Makefile.am结合 实现条件编译
)
1、configure.ac代码
#在configure.in中自定义一个./configure命令选项
#AC_ARG_ENABLE (feature, help-string, [action-if-given], [action-if-not-given]) 如果给定的选项,就执行action-if-given,否则执行action-if-not-given 最后两个参数可以缺省
#使用AC_ARG_ENABLE宏可以为configure增加–enable-feature 或者 –disable-feature这样的选项。
举例:
AC_ARG_ENABLE( compile_asan,
[–enable-compile-asan compile with asan.],
[ case “${enableval}” in
yes) compile_asan=true ;;
no) compile_asan=false ;;
*) AC_MSG_ERROR(bad value ${enableval} for -enable-compile-asan) ;;
esac],
[compile_asan=“false”]
)
#如果compile_asan的值为true,那么将会定义DPI_ENABLE_ASAN这个宏
AM_CONDITIONAL(DPI_ENABLE_ASAN, test “x$compile_asan” = “xyes”
2、Makefile.am代码
if DPI_ENABLE_ASAN
CFLAGS += -fsanitize=address -DTHREAD_TEST
LIBS += -lpthread
endif
3、cpp代码
#ifdef THREAD_TEST
。。。
pthread_t thread_id;
pthread_create(&thread_id, NULL, threadTest, NULL);
pthread_join(thread_id, NULL);
。。。
#endif
4、总结
我们在上面通过AC_ARG_ENABLE增加了–enable-compile-asan这个编译选项,在执行configure时,将是这样的一种方式执行./configure --enable-compile-asan=yes或者–enable-compile-asan=no
1、当–enable-compile-asan=yes,将是compile_asan=true,接着AM_CONDITIONAL将会因为compile_asan=true而#define DPI_ENABLE_ASAN;
2、当–enable-compile-asan=no,将是compile_asan=false,接着AM_CONDITIONAL没有进行任何操作;
3、当我们在./configure时忽略–enable-compile-asan选项,AC_ARG_ENABLE定义的第四个参数为默认值,即[compile_asan=false]