使用 stdlibc++ 4.7 启用 C++11 时,clang 会出错,而 gcc 编译正常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10308167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:52:51  来源:igfitidea点击:

When enabling C++11 with stdlibc++ 4.7, clang error out, while gcc compiles fine

c++macosgccc++11clang

提问by Negative Zero

I have been trying to get C++11 to work, after browsing different websites and Q/A, i am still having trouble with. I want to use clang with libstdc++. It is indicated in the clang status that it's supported with patch - http://clang.llvm.org/libstdc++4.7-clang11.patch. I download the gcc4.7 from macports and made corresponding changes in the headers for gcc4.7

我一直在尝试让 C++11 工作,在浏览不同的网站和问答后,我仍然遇到问题。我想在 libstdc++ 中使用 clang。它在 clang 状态中表明它受补丁支持 - http://clang.llvm.org/libstdc++4.7-clang11.patch。我从 macports 下载了 gcc4.7 并在 gcc4.7 的头文件中做了相应的更改

The reason that i don't uses libc++ is because ABI compatibilities between libc++ and libstdc++, indicated by this thread: Why can't clang with libc++ in c++0x mode link this boost::program_options example?

我不使用 libc++ 的原因是因为 libc++ 和 libstdc++ 之间的 ABI 兼容性,由该线程指出:为什么在 c++0x 模式下无法使用 libc++ 链接此 boost::program_options 示例?

OK, after everything is done, i tested my setup with the following code:

好的,一切都完成后,我使用以下代码测试了我的设置:

#include <mutex>
#include <thread>

int main ( ) {
    std::mutex myMutext;
    return 0;
}

I am expecting that include should work under c++11.

我期待 include 应该在 c++11 下工作。

So here is how I compile it with: with GCC

所以这是我编译它的方式:使用 GCC

g++ -std=c++11 -I/opt/local/include/gcc47/c++ -L/opt/local/lib/gcc47 main.cpp -o main

Compile successfully

编译成功

with Clang

与铿锵

clang++ -std=c++11 -I/opt/local/include/gcc47/c++ -L/opt/local/lib/gcc47 main.cpp -o main

I am getting this error:

我收到此错误:

@work:boostTest$ clang++ -std=c++11 -I/opt/local/include/gcc47/c++ -L/opt/local/lib/gcc47 main.cpp -o main
In file included from main.cpp:1:
In file included from /opt/local/include/gcc47/c++/mutex:38:
In file included from /opt/local/include/gcc47/c++/tuple:37:
In file included from /opt/local/include/gcc47/c++/utility:70:
/opt/local/include/gcc47/c++/bits/stl_relops.h:72:3: error: unknown type name '_GLIBCXX_BEGIN_NAMESPACE_VERSION'
  _GLIBCXX_BEGIN_NAMESPACE_VERSION
  ^
/opt/local/include/gcc47/c++/bits/stl_relops.h:86:5: error: expected unqualified-id
    template <class _Tp>
    ^
In file included from main.cpp:1:
In file included from /opt/local/include/gcc47/c++/mutex:38:
In file included from /opt/local/include/gcc47/c++/tuple:37:
In file included from /opt/local/include/gcc47/c++/utility:71:
In file included from /opt/local/include/gcc47/c++/bits/stl_pair.h:61:
/opt/local/include/gcc47/c++/bits/move.h:38:1: error: unknown type name '_GLIBCXX_BEGIN_NAMESPACE_VERSION'
_GLIBCXX_BEGIN_NAMESPACE_VERSION
^
/opt/local/include/gcc47/c++/bits/move.h:45:3: error: expected unqualified-id
  template<typename _Tp>
  ^
In file included from main.cpp:1:
In file included from /opt/local/include/gcc47/c++/mutex:38:
In file included from /opt/local/include/gcc47/c++/tuple:37:
In file included from /opt/local/include/gcc47/c++/utility:71:
In file included from /opt/local/include/gcc47/c++/bits/stl_pair.h:61:
In file included from /opt/local/include/gcc47/c++/bits/move.h:57:
/opt/local/include/gcc47/c++/type_traits:41:1: error: unknown type name '_GLIBCXX_BEGIN_NAMESPACE_VERSION'
_GLIBCXX_BEGIN_NAMESPACE_VERSION
^
/opt/local/include/gcc47/c++/type_traits:55:3: error: expected unqualified-id
  template<typename _Tp, _Tp __v>
  ^
/opt/local/include/gcc47/c++/type_traits:65:11: error: unknown type name 'integral_constant'
  typedef integral_constant<bool, true>     true_type;
          ^
/opt/local/include/gcc47/c++/type_traits:65:28: error: expected unqualified-id
  typedef integral_constant<bool, true>     true_type;
                           ^
/opt/local/include/gcc47/c++/type_traits:68:11: error: unknown type name 'integral_constant'
  typedef integral_constant<bool, false>    false_type;
          ^
/opt/local/include/gcc47/c++/type_traits:68:28: error: expected unqualified-id
  typedef integral_constant<bool, false>    false_type;
                           ^
/opt/local/include/gcc47/c++/type_traits:71:36: error: expected ';' after top level declarator
    constexpr _Tp integral_constant<_Tp, __v>::value;
                                   ^
/opt/local/include/gcc47/c++/type_traits:83:14: error: expected class name
    : public false_type
             ^
/opt/local/include/gcc47/c++/type_traits:106:14: error: expected class name
    : public true_type
             ^
/opt/local/include/gcc47/c++/type_traits:126:14: error: unknown template name 'integral_constant'
    : public integral_constant<bool, !_Pp::value>
             ^
/opt/local/include/gcc47/c++/type_traits:126:38: error: expected class name
    : public integral_constant<bool, !_Pp::value>
                                     ^
/opt/local/include/gcc47/c++/type_traits:142:14: error: expected class name
    : public false_type { };
             ^
/opt/local/include/gcc47/c++/type_traits:146:14: error: expected class name
    : public true_type { };
             ^
/opt/local/include/gcc47/c++/type_traits:151:14: error: unknown template name 'integral_constant'
    : public integral_constant<bool, (__is_void_helper<typename
             ^
/opt/local/include/gcc47/c++/type_traits:151:38: error: expected class name
    : public integral_constant<bool, (__is_void_helper<typename
                                     ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

I am using clang version:

我正在使用 clang 版本:

Apple clang version 4.0 (tags/Apple/clang-418.2.41) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.3.0
Thread model: posix

Am I doing something wrong? or is this a clang problem with latest gcc 4.7 libstc++?

难道我做错了什么?或者这是最新的gcc 4.7 libstc ++的叮当声问题?

采纳答案by Jonathan Wakely

Why are you saying -I/opt/local/include/gcc47/c++?

你为什么这么说-I/opt/local/include/gcc47/c++

That should not be necessary with either GCC or Clang, and will not work. Not all libstdc++ headers are under that path, there are some essential headers elsewhere that define things like _GLIBCXX_BEGIN_NAMESPACE_VERSION

这对于 GCC 或 Clang 都不是必需的,并且不会起作用。并非所有 libstdc++ 头文件都在该路径下,其他地方有一些基本头文件定义了诸如_GLIBCXX_BEGIN_NAMESPACE_VERSION

It doesn't fail with GCC because GCC already knows how to find the other headers, so it's redundant to explicitly use -Iand -Loptions. It doesn't work with Clang because you are only telling it how to find some of the headers it needs, but not telling it how to find the rest.

GCC 不会失败,因为 GCC 已经知道如何找到其他标头,因此显式使用-I-L选项是多余的。它不适用于 Clang,因为您只是告诉它如何找到它需要的一些标头,而不是告诉它如何找到其余的标头。

Stop trying to override the compiler's standard library paths, let it use the built-in paths it already knows about.

停止尝试覆盖编译器的标准库路径,让它使用它已经知道的内置路径。

回答by Yasuhiro Fujii

I'm using clang-3.1 with gcc4.6 libstdc++ on FreeBSD 9.0/AMD64. It works with these options:

我在 FreeBSD 9.0/AMD64 上使用带有 gcc4.6 libstdc++ 的 clang-3.1。它适用于以下选项:

-I/usr/local/lib/gcc46/include/c++ \
-I/usr/local/lib/gcc46/include/c++/x86_64-portbld-freebsd9.0 \
-L/usr/local/lib/gcc46

I guess your problem may be solved to use these options:

我想您的问题可以通过使用这些选项来解决:

-I/opt/local/include/gcc47/c++ \
-I/opt/local/include/gcc47/c++/x86_64-apple-darwin11.3.0 \
-L/opt/local/lib/gcc47

回答by gluk47

You can use the special option -gcc-toolchain, which is implicitly set by --with-gcc-toolchainwhen you compile clang. It's a bit easier than recompile clang when you want to use another GCC libraries :)

您可以使用在编译 clang 时隐式设置的特殊选项-gcc-toolchain--with-gcc-toolchain。当您想使用另一个 GCC 库时,它比重新编译 clang 容易一些:)

Like that:

像那样:

~/clang/trunk/bin/clang++ main.cc -gcc-toolchain ~/gcc/trunk -o main

Or, in your case (I know it's 4 years old :)) it seems to be

或者,在你的情况下(我知道它已经 4 岁了:))它似乎是

clang++ main.cpp -o main -gcc-toolchain /opt/local

The 'toolchain' folder should contain 'include' and 'lib' folders. Both compiler and linker use this option. Be careful: --gcc-toolchainis not a valid option, use one dash as the prefix (even though the llvm wikistates otherwise?— I checked it on clang 3.8 trunk).

'toolchain' 文件夹应该包含 'include' 和 'lib' 文件夹。编译器和链接器都使用此选项。小心:--gcc-toolchain不是一个有效的选项,使用一个破折号作为前缀(即使llvm wiki另有说明? - 我在 clang 3.8 主干上检查了它)。