如何使用autotools使用clang和选项-std = c ++ 11编译项目

时间:2016-06-05 16:19:57

标签: c++ c c++11 clang autotools

我正在使用C和C ++代码开发软件。我最近在c ++ 11标准中添加了一些代码。 在configure.ac中我写道:

for f in '-std=c++11' '-std=c++11 -stdlib=libc++'
do
    AX_CHECK_COMPILE_FLAG([$f], [CXXFLAGS="$CXXFLAGS $f" stdpass=true], [], [], [])
${stdpass-false} && break
done
if ! "${stdpass-false}"; then
    AC_MSG_ERROR([Unable to turn on C++11 mode with this compiler])
fi

使用gcc我没有问题,一切顺利,选项-std = c ++ 11仅适用于g ++而不适用于gcc。 如果我尝试配置:

CC=clang ./configure

我有以下错误:

checking whether C compiler accepts -std=c++11... no
checking whether C compiler accepts -std=c++11 -stdlib=libc++... no
configure: error: Unable to turn on C++11 mode with this compiler

就好像该选项是在C编译器上应用的,而不仅仅是在clang ++上(就像使用gcc一样)。

有人可以帮我弄清楚我做错了什么。

1 个答案:

答案 0 :(得分:1)

好的,经过一些调查后我得到了答案。 首先,在configure.ac中我必须设置我使用的语言:

AC_LANG([C])
AC_LANG([C++])

然后,已经有一个autoconf宏来检查C ++编译器中的C ++ 11支持:AX_CXX_COMPILE_STDCXX_11

所以,请点击以下链接:https://www.gnu.org/software/automake/manual/html_node/Local-Macros.html, 我必须创建一个m4文件夹并将宏定义放在里面。继续进行的最佳方法是仅下载更通用的ax_cxx_compile_stdcxx.m4文件(而不是ax_cxx_compile_stdcxx_11.m4)。 所以,总是在configure.ac中写道:

AC_CONFIG_MACRO_DIR([m4])

m4_include([m4/ax_cxx_compile_stdcxx.m4])
AX_CXX_COMPILE_STDCXX(11, noext, mandatory)

和Voilà。 一切都很好,至少在我测试过的机器上。

相关问题