Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I'm running into problems compiling on Ubuntu 16.04 LTS (server). It compiles okay if I don't include the -std=c++11 bit. Clang version is 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. 
like image 947
Darin Avatar asked May 08 '16 03:05

Darin


People also ask

How do you compile with clang?

2.4. To compile a C++ program on the command line, run the clang++ compiler as follows: $ scl enable llvm-toolset-6.0 'clang++ -o output_file source_file ...' This creates a binary file named output_file in the current working directory. If the -o option is omitted, the clang++ compiler creates a file named a.

What is clang in C?

clang is a C, C++, and Objective-C compiler which encompasses preprocessing, parsing, optimization, code generation, assembly, and linking. Depending on which high-level mode setting is passed, Clang will stop before doing a full link.

Should I use clang or G ++?

Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.


2 Answers

Until the Debian bug mentioned in Mike Kinghan's reply is fixed, just adding the missing (but required) noexcept specification to the ctor definition manually allows to work around the problem, i.e. you could just add

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

after the line 1938 of /usr/include/c++/v1/string.

like image 55
VZ. Avatar answered Sep 21 '22 13:09

VZ.


You have installed libc++-dev on ubuntu 16.04 in the (correct) expectation that it ought to let you build with clang++ using libc++ and its headers for your standard library.

It ought to, but in the presence of std=c++11 (or later standard), it doesn't, because of Debian bug #808086, which you have run into.

If you wish to compile with clang++ to the C++11 standard or later, then until ubuntu gets a fix for this you will have to do so without libc++, using libstdc++ (the GNU C++ standard library) instead, which is the default behaviour.

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

or:

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

will work.

like image 30
Mike Kinghan Avatar answered Sep 22 '22 13:09

Mike Kinghan