3

我正在使用 C++ Primer,第 5 版学习 C++。

我正在尝试使用 clang++ 编译一个具有 C++11 功能的简单 c++ 程序,但我遇到了应该是有效代码的编译错误。

这是一个例子:

#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;
}

这是完整的错误:

$ 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.

我在 Mac 上,10.6.8,我用自制软件构建了 llvm/clang,并带有选项:

brew install llvm -v --all-targets --rtti --shared --with-asan --with-clang --use-clang

我究竟做错了什么?

4

2 回答 2

6

该错误告诉您没有采用初始化列表的 std::vector 构造函数。问题是您使用的标准库实现尚未针对 C++11 进行更新。您使用的是 gcc 4.2 版本的 libstdc++,它是在 C++11 之前几年编写的。

您必须切换到已针对 C++11 更新的库。如果您安装了 libc++,那么您可以只使用 flag -stdlib=libc++。我认为 libc++ 二进制文件是 OS X 10.6.8 的一部分,但您还需要标头。也许 homebrew 有一个配方,或者您可以从以下网址下载它们:http: //libcxx.llvm.org/并将它们粘贴在适当的位置以便找到并与-stdlib=libc++.

或者,您可以手动省略普通的 C++ 标准库头文件(在这种情况下是 gcc 4.2 头文件)-nostdinc++,手动为粘贴头文件的位置添加包含目录,为二进制文件所在的位置添加库搜索目录(您甚至可以构建自己的二进制文件)并添加链接标志-lc++

于 2013-02-13T23:26:16.823 回答
5

我不确定这是否是好的规范,但我会回答我自己的问题。我希望这对其他人有帮助。

(我只是一个初学者,所以如果我做错了什么,请纠正我。)

构建上述示例的唯一方法是:

-std=c++11 -stdlib=libc++ -nostdinc++ -I/path/to/new/libcxx/include

问题是雪豹,正如其他人在这里所说,它有一个非常旧的 gcc 版本。并且使用上面的选项无法链接,因为ld: library not found for -lc++.

所以我不得不构建一个最新版本的 libcxx。有一个 macports 端口,但没有自制配方。我手动构建了它,但有些事情我必须绕过才能成功。

我假设您已经使用 homebrew 安装了 llvm 并且您的路径设置正确:which clang++ -> /usr/local/bin/clang++.

首先获取 libc++abi:svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi

然后得到 libc++: svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx

编辑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.)

现在构建 libc++abi:

- cd libc++abi/lib 
- export TRIPLE=-apple-
- ./buildit

我更喜欢用自制软件来管理我所有的 3rd 方库,所以这是可选的:

- 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

现在编辑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 \
        ..."

现在构建 libc++:

- cd libc++abi/lib 
- export TRIPLE=-apple-
- export MACOSX_DEPLOYMENT_TARGET=10.6
- ./buildit

再次可选:

- 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++

我现在可以使用以下方法构建前面的示例:

clang++ -std=c++11 -stdlib=libc++ -nostdinc++ -I/usr/local/include/libc++ -O2 -g -v test.cpp -o test 
于 2013-02-14T11:13:41.423 回答