Issam T.
Issam T.

Reputation: 1697

Apple Clang; using C++11 with libstdc++

I have an issue when compiling a simple Hello file with an empty function taking initializer_list argument when using both -stdlib=libstdc++ and -std=c++11

If I use only -std=c++11 (which means compiling with libc++)

then the file compiles and prints Hello!

If I comment function_test and I use both -std=c++11 and -stdlib=libstdc++

then the file compiles and prints Hello!

If I keep the function function_test and I use both -std=c++11 and -stdlib=libstdc++

then I get the following error:

$ g++ -stdlib=libstdc++  -std=c++11   -o test test.cpp
test.cpp:1:10: fatal error: 'initializer_list' file not found
#include <initializer_list>
          ^
1 error generated.

Here is my file

#include <initializer_list>
#include <iostream>
using namespace std;

void function_test(initializer_list<int> something){}

int main(int argc, char * argv[])
{
   cout << "Hello!" << endl;
   function_test({0});
   return 0;
}

Here is my apple clang version

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix

Upvotes: 3

Views: 3057

Answers (2)

Issam T.
Issam T.

Reputation: 1697

I found out that upgrading to a newer version of libstdc++ is just not possible with apple-llvm(clang). So using some features of C++11 with libstdc++ is not possible. The reason is this one:

Mainline libstdc++ has switched to GPL3, a license which the developers of libc++ cannot use. libstdc++ 4.2 (the last GPL2 version) could be independently extended to support C++11, but this would be a fork of the codebase (which is often seen as worse for a project than starting a new independent one). Another problem with libstdc++ is that it is tightly integrated with G++ development, tending to be tied fairly closely to the matching version of G++.

source: http://libcxx.llvm.org/docs/

Thanks to all the answers/comments that helped me reach the answer.

Upvotes: 1

Baum mit Augen
Baum mit Augen

Reputation: 50043

--with-gxx-include-dir=/usr/include/c++/4.2.1
                                       ^^^^^^^

Notice the "4.2". Your libstdc++ is way to old for C++11. Upgrade it to some 5.x version for full C++11 support.

Upvotes: 6

Related Questions