2012-05-02 40 views
5

Tôi đang cố gắng khởi tạo tính năng danh sách trong C++ 11 bằng cách sử dụng cài đặt clang của MacPorts. Khi biên dịch mã đơn giản này:MacPorts Clang 3.1 thiếu cài đặt Thư viện Chuẩn?

#include <vector> 

int main() 
{ 
    std::vector<int> a {1, 3, 5}; 
    return 0; 
} 

Tôi đã nhận lỗi này:

stephen-chus-mac-pro:~ stephenchu$ clang -std=c++0x -c text.cxx -I/opt/local/include -v 
clang version 3.1 (trunk 154872) 
Target: x86_64-apple-darwin10.8.0 
Thread model: posix 
"/opt/local/libexec/llvm-3.1/bin/clang" -cc1 -triple x86_64-apple-macosx10.6.0 -emit-obj -mrelax-all -disable-free -main-file-name text.cxx -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 127.2 -v -coverage-file text.o -resource-dir /opt/local/libexec/llvm-3.1/bin/../lib/clang/3.1 -I /opt/local/include -fmodule-cache-path /var/folders/UL/ULMxdJJtEQuI+WuToNAFpk+++TI/-Tmp-/clang-module-cache -std=c++0x -fdeprecated-macro -fdebug-compilation-dir /Users/stephenchu -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o text.o -x c++ text.cxx 
clang -cc1 version 3.1 based upon LLVM 3.1svn default target x86_64-apple-darwin10.8.0 
#include "..." search starts here: 
#include <...> search starts here: 
/opt/local/include 
/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 
/opt/local/libexec/llvm-3.1/bin/../lib/clang/3.1/include 
/usr/include 
/System/Library/Frameworks (framework directory) 
/Library/Frameworks (framework directory) 
End of search list. 
text.cxx:5:19: error: no matching constructor for initialization of 
     'std::vector<int>' 
     std::vector<int> a {1, 3, 5}; 
         ^~~~~~~~~~ 
/usr/include/c++/4.2.1/bits/stl_vector.h:255:9: note: candidate constructor 
     [with _InputIterator = int] not viable: no known conversion from 'int' to 
     'const allocator_type' (aka 'const std::allocator<int>') 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 'int' to 'const allocator_type' (aka 
     'const std::allocator<int>') for 3rd 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: requires at most 1 argument, but 3 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 1 argument, but 3 were provided 
     vector(const vector& __x) 
    ^
1 error generated. 

Dường như để bao gồm các tiêu đề từ hệ thống gcc (4.2). Và tôi không thể tìm thấy bất kỳ tiêu đề cụ thể nào. Tôi nên cài đặt gói nào từ MacPorts để làm việc này? Hay tôi cần cài đặt và xây dựng libC++ của clang?

+1

[Tôi thực sự hy vọng bạn có nghĩa là "thư viện chuẩn C++", không phải STL.] (Http://stackoverflow.com/questions/5205491/whats-this-stl-vs-c-standard-library-fight-all- about/5205571 # 5205571) – Griwes

+0

@Griwes yes. Tôi đã chỉnh sửa câu hỏi để phản ánh điều đó. Xin lỗi vì sai thời hạn. –

Trả lời

3

Trước tiên, bạn cần phải đặt thư viện chuẩn với '-stdlib = libC++'

Sau đó, bạn có thể cần tiêu đề được cập nhật. Các tiêu đề libC++ mới nhất sẽ hoạt động với libC++ libs được cài đặt với OS X 10.7 trở lên.

Tôi không biết rằng macports có libC++, nhưng vì tất cả những gì bạn cần là tiêu đề, bạn có thể tải chúng xuống và thả chúng vào thư mục bao gồm cạnh bất kỳ nơi nào được cài đặt.

+0

Cảm ơn. Tôi quên đề cập đến điều này là với OS X 10.6 và Xcode 4.2 mà không có libC++ (không phải là tôi có thể tìm thấy). Tôi sẽ cài đặt tiêu đề libC++ và xem. –

+0

Được xây dựng và cài đặt libC++ từ nguồn và nó hoạt động ngay bây giờ. Cảm ơn. –

Các vấn đề liên quan