No matching constructor to initialize 'vector <string>' with clang ++ 3.2
I am learning C ++ with C ++ Primer, 5th ed ..
I am trying to compile a simple C ++ program with C ++ 11 functions using clang ++, but I am getting compilation errors for correct code.
This is an example:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
int n = 0;
auto *p = &n; //<-- this compiles
cout << *p << endl;
vector<string> articles = {"a", "an", "the"}; //<-- this fails; copied from the book
return 0;
}
And this is a complete error:
$ clang++ -std=c++11 -v test.cpp -o test
clang version 3.2 (tags/RELEASE_32/final)
Target: x86_64-apple-darwin10.8.0
Thread model: posix
"/usr/local/Cellar/llvm/3.2/bin/clang" -cc1 -triple x86_64-apple-macosx10.6.0 -emit-obj -mrelax-all -disable-free -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 127.2 -v -resource-dir /usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2 -fmodule-cache-path /var/folders/pk/pkIeYbRaF-yeeUH3Q6Q5AE+++TI/-Tmp-/clang-module-cache -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/Oton/Desktop -ferror-limit 19 -fmessage-length 150 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.6.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/pk/pkIeYbRaF-yeeUH3Q6Q5AE+++TI/-Tmp-/test-w65CaF.o -x c++ test.cpp
clang -cc1 version 3.2 based upon LLVM 3.2svn default target x86_64-apple-darwin10.8.0
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.2.1
/usr/include/c++/4.2.1/i686-apple-darwin10/x86_64
/usr/include/c++/4.2.1/backward
/usr/include/c++/4.0.0
/usr/include/c++/4.0.0/i686-apple-darwin8
/usr/include/c++/4.0.0/backward
/usr/local/include
/usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
test.cpp:13:18: error: no matching constructor for initialization of 'vector<string>'
vector<string> articles = {"a", "an", "the"};
^ ~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.2.1/bits/stl_vector.h:255:9: note: candidate constructor [with _InputIterator = const char *] not viable: no known conversion from
'const char [4]' to 'const allocator_type' (aka 'const std::allocator<std::basic_string<char> >') for 3rd argument
vector(_InputIterator __first, _InputIterator __last,
^
/usr/include/c++/4.2.1/bits/stl_vector.h:213:7: note: candidate constructor not viable: no known conversion from 'const char [2]' to 'size_type'
(aka 'unsigned long') for 1st argument
vector(size_type __n, const value_type& __value = value_type(),
^
/usr/include/c++/4.2.1/bits/stl_vector.h:201:7: note: candidate constructor not viable: allows at most single argument '__a', but 3 arguments were
provided
vector(const allocator_type& __a = allocator_type())
^
/usr/include/c++/4.2.1/bits/stl_vector.h:231:7: note: candidate constructor not viable: requires single argument '__x', but 3 arguments were provided
vector(const vector& __x)
^
1 error generated.
I am on mac, 10.6.8, I created llvm / clang with homebrew with options:
brew install llvm -v --all-targets --rtti --shared --with-asan --with-clang --use-clang
What am I doing wrong?
source to share
The error tells you that the std :: vector constructor does not have an initializer list. The problem is that the standard implementation of the library you are using has not been updated for C ++ 11. You are using libstdC ++ gcc 4.2 version written several years before C ++ 11.
You need to switch to the library updated for C ++ 11. If you have libc ++ installed you can just use the flag -stdlib=libc++
. I think the libc ++ binary is part of OS X 10.6.8, but you will need the headers as well. Maybe have a homebrew recipe, or you can just download them from: http://libcxx.llvm.org/ and paste them into the appropriate location to be found and it will be used from -stdlib=libc++
.
Alternatively, you can manually exclude the usual standard C ++ library headers (in this case gcc 4.2) with -nostdinc++
, manually add the include directory wherever you insert the headers, add the library search directory wherever the binary is (you can even build yourself binary) and add a link flag -lc++
.
source to share
I'm not sure if this is a good norm, but I'll answer my own question. Hope this helps others.
(I'm just a beginner, so if I am wrong, please correct me.)
The only way to build the example above:
-std=c++11 -stdlib=libc++ -nostdinc++ -I/path/to/new/libcxx/include
The problem is Snow Leopard, which others have said has a very old version of gcc. And the use of the options above cannot be related due to ld: library not found for -lc++
.
So I had to build the latest libcxx. There is a port of macports but no recipe for homebrew. I built it by hand, but there are some things I needed to do in order to succeed.
I assume that you have installed llvm with homebrew, and your path is set up correctly: which clang++ -> /usr/local/bin/clang++
.
First get libc ++ abi: svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi
then get libS ++ svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
.
Edit libc++abi/lib/buildit
:
EXTRA_FLAGS="...
-I/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin10/4.2.1/include \
-I/PATH/TO/libcxx/include"
(Add the last two lines, note that the last include should point to the include folder inside libcxx from svn.)
LDSHARED_FLAGS="...-install_name /usr/local/lib/libc++abi.dylib"
(My libc++abi.dylib will reside inside /usr/local/lib.)
Now let's create lib ++ abi:
- cd libc++abi/lib
- export TRIPLE=-apple-
- ./buildit
I prefer to manage all my third party libraries with homebrew, so this is optional:
- mkdir -p /usr/local/Cellar/libc++abi/HEAD/lib
- mkdir -p /usr/local/Cellar/libc++abi/HEAD/include
- cp -a *.dylib /usr/local/Cellar/libc++abi/HEAD/lib/
- cp -R /path/to/libcxxabi/include/ /usr/local/Cellar/libc++abi/HEAD/include/libc++abi
(Note the / after include!)
- brew link libc++abi
Now edit libc++/lib/buildit
:
if [ "$MACOSX_DEPLOYMENT_TARGET" == "10.6" ]
then
EXTRA_FLAGS="... -I/usr/local/include/libc++abi"
LDSHARED_FLAGS="...
-install_name /usr/local/lib/libc++.1.dylib \
..."
Now let's create libC ++:
- cd libc++abi/lib
- export TRIPLE=-apple-
- export MACOSX_DEPLOYMENT_TARGET=10.6
- ./buildit
Again, optional:
- mkdir -p /usr/local/Cellar/libc++/HEAD/lib
- cp -a *.dylib /usr/local/Cellar/libc++/HEAD/lib
- mkdir -p /usr/local/Cellar/libc++/HEAD/include
- cp -R /path/to/libcxx/include/ /usr/local/Cellar/libc++/HEAD/include/libc++
- brew link libc++
Now I can create the previous example with
clang++ -std=c++11 -stdlib=libc++ -nostdinc++ -I/usr/local/include/libc++ -O2 -g -v test.cpp -o test
source to share