【问题标题】:Clang can't compile programs using the <functional> header anymoreClang 无法再使用 <functional> 标头编译程序
【发布时间】:2017-12-12 02:46:32
【问题描述】:

我在运行 ArchLinux 的系统上使用 clang 版本 4.0.0,它一直运行良好,但最近我无法编译使用某些 STL 头文件的程序了!

详情:

clang --version的输出:

 clang version 4.0.0 (tags/RELEASE_400/final)
 Target: x86_64-unknown-linux-gnu
 Thread model: posix
 InstalledDir: /usr/bin

gcc --version 的输出:

 gcc (GCC) 7.1.1 20170528

示例:

我尝试编译以下琐碎的程序:

#include <functional>

int main()
{
    return 0;
}

我正在使用以下命令:

clang++ -std=c++1z test.cxx

结果是失败:

In file included from test.cxx:3:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/functional:60:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/unordered_map:47:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/bits/hashtable.h:37:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/bits/node_handle.h:39:
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/optional:1032:27: error: use of
      class template 'optional' requires template arguments
  template <typename _Tp> optional(_Tp) -> optional<_Tp>;
                          ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/optional:451:11: note: template
      is declared here
    class optional
          ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/optional:1032:40: error: 
      expected ';' at end of declaration
  template <typename _Tp> optional(_Tp) -> optional<_Tp>;
                                       ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/optional:1032:41: error: cannot
      use arrow operator on a type
  template <typename _Tp> optional(_Tp) -> optional<_Tp>;
                                        ^
3 errors generated.

这是 STL 中的错误还是我的设置搞砸了?

【问题讨论】:

  • 您是否尝试过重新排列包含的顺序?这就是有时为我带来程序的原因。或者尝试删除 #include &lt;iostream&gt;#include &lt;type_traits&gt; 是否有助于或导致不同的错误
  • 您似乎在使用 gcc c++ 标准库。也许尝试使用铿锵声。类似-stdlib=libc++.
  • @ThFl 我删除了除&lt;functional&gt; 以外的所有其他标头,但问题仍然存在。我将编辑原始问题。不过还是谢谢。
  • @nshct 您能否将clang --versiongcc --version 的输出添加到您的问题中(主要针对未来的读者)?
  • @Holt 是的,我已经添加了它。谢谢。

标签: c++ stl clang c++17


【解决方案1】:

您的设置搞砸了。很有趣,我也有同样的问题。

当您升级到 gcc 7.1.1 时,libstdc++(它是 gcc 的标准库)随之更新以提供 C++17 的新功能。使用 gcc,这是可行的,因为它几乎完全支持 C++17。

但是clang没有。提示是 -std=c++1z 标志而不是 gcc 的 -std=c++17 标志。 Clang 缺少deduction guides,当它在 libstdc++ 中遇到它们时,它不知道如何处理它们。

您可以从 ALA 安装旧的 libstdc++ 包,也可以下载/使用 LLVM 的标准库 libc++,它自然只有部分 C++17 功能。

【讨论】:

  • 你刚刚解决了我整天头疼的问题。 Clang 和 clang-tidy 都被 std::thread{&amp;Foo::bar, this} 呛到了。原来它在类模板扣除上失败了。我现在可以吻你了。
猜你喜欢
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
相关资源
最近更新 更多