得到一个基本的c ++程序,在Ubuntu 16上使用clang ++进行编译

Dar*_*rin 22 c++ ubuntu c++11 clang++

我遇到了在Ubuntu 16.04 LTS(服务器)上编译的问题.如果我不包括该-std=c++11位,它编译好.Clang版本是3.8.

>cat foo.cpp
#include <string>
#include <iostream>
using namespace std;

int main(int argc,char** argv) {
    string s(argv[0]);
    cout << s << endl;
}


>clang++ -std=c++11 -stdlib=libc++ foo.cpp
In file included from foo.cpp:1:
/usr/include/c++/v1/string:1938:44: error: 'basic_string<_CharT, _Traits, _Allocator>' is missing exception specification
      'noexcept(is_nothrow_copy_constructible<allocator_type>::value)'
basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
                                           ^
/usr/include/c++/v1/string:1326:40: note: previous declaration is here
    _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
                                       ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

VZ.*_*VZ. 22

直到Mike Kinghan的回复中提到的Debian bug被修复,只需noexcept手动将缺少的(但必需的)规范添加到ctor定义中就可以解决问题,即你可以添加

#if _LIBCPP_STD_VER <= 14
    _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
#else
    _NOEXCEPT
#endif
Run Code Online (Sandbox Code Playgroud)

1938年之后/usr/include/c++/v1/string.

  • 请注意,在我进行此更改后,我还必须安装libc ++ abi-dev,然后安装ln -s /usr/include/libcxxabi/__cxxabi_config.h /usr/include/c++/v1/__cxxabi_config.h (10认同)

Mik*_*han 20

你已经libc++-dev在ubuntu 16.04上安装了(正确的)期望它应该让你用标准库的clang++使用libc++和它的标题构建.

应该,但在std=c++11(或更高标准)的存在,它不,因为Debian bug#808086,你遇到过.

如果您希望使用clang++C++ 11标准或更高版本进行编译,那么在ubuntu为此获得修复之前,您将不得不libc++使用 libstdc++(GNU C++标准库),这是默认行为.

clang++ -std=c++11 foo.cpp
Run Code Online (Sandbox Code Playgroud)

要么:

clang++ -std=c++11 -stdlib=libstdc++ foo.cpp
Run Code Online (Sandbox Code Playgroud)

将工作.

  • 我在Launchpad上重新打开了同样的错误[这里](https://bugs.launchpad.net/ubuntu/+source/libc++/+bug/1610168).虽然在Debian中这是一个小小的不便,因为(截至今天)它只在实验中发布了Clang 3.8,Ubuntu 16.04附带了Clang 3.8和一个破解的libc ++,这完全是疯了. (5认同)