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

How to compile project with clang and the option -std=c++11, using 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 中所做的那样)。

谁能帮我弄清楚我做错了什么。

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

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

那么,在 C++ 编译器中已经有一个检查 C++11 支持的 autoconf 宏:AX_CXX_COMPILE_STDCXX_11

因此,在 link 之后: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)

瞧瞧。 一切正常,至少在我测试过的机器上是这样。