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

loc*_*ock 5 c c++ autotools clang c++11

我正在使用 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
Run Code Online (Sandbox Code Playgroud)

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

CC=clang ./configure
Run Code Online (Sandbox Code Playgroud)

我有以下错误:

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
Run Code Online (Sandbox Code Playgroud)

这就像该选项应用于 C 编译器而不是仅应用于 clang++(就像它在 gcc 中所做的那样)。

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

loc*_*ock 5

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

\n\n
AC_LANG([C])\nAC_LANG([C++])\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

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

\n\n
AC_CONFIG_MACRO_DIR([m4])\n
Run Code Online (Sandbox Code Playgroud)\n\n

\n\n
m4_include([m4/ax_cxx_compile_stdcxx.m4])\nAX_CXX_COMPILE_STDCXX(11, noext, mandatory)\n
Run Code Online (Sandbox Code Playgroud)\n\n

和 Voil\xc3\xa0。\n一切正常,至少在我测试过的机器上是这样。

\n