繁体   English   中英

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

[英]get a basic c++ program to compile using clang++ on Ubuntu 16

我遇到了在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.

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

#if _LIBCPP_STD_VER <= 14
    _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
#else
    _NOEXCEPT
#endif

/usr/include/c++/v1/string 1938行之后。

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

应该 ,但在std=c++11 (或更高版本标准)的情况下,它不会,因为Debian bug#808086 ,你遇到过。

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

clang++ -std=c++11 foo.cpp

要么:

clang++ -std=c++11 -stdlib=libstdc++ foo.cpp

将工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM