簡體   English   中英

使用stdlibc ++ 4.7啟用C ++ 11時,clang錯誤輸出,而gcc編譯正常

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

我一直在嘗試讓C ++ 11工作,在瀏覽了不同的網站和Q / A之后,我仍然遇到了麻煩。 我想和libstdc ++一起使用clang。 它在clang狀態中表示它受補丁支持 - http://clang.llvm.org/libstdc++4.7-clang11.patch 我從macports下載gcc4.7並在gcc4.7的頭文件中做了相應的更改

我不使用libc ++的原因是因為libc ++和libstdc ++之間的ABI兼容性,由這個線程表示: 為什么不能用c ++ 0x模式中的libc ++來鏈接這個boost :: program_options示例?

好的,一切都完成后,我使用以下代碼測試了我的設置:

#include <mutex>
#include <thread>

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

我期待include應該在c ++ 11下工作。

所以這就是我用GCC編譯它的方法

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

編譯成功

與Clang

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

我收到此錯誤:

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

我正在使用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

難道我做錯了什么? 或者這是最新的gcc 4.7 libstc ++的鏗鏘問題?

你為什么這么說-I/opt/local/include/gcc47/c++

對於GCC或Clang來說,這不應該是必要的,並且不會起作用。 並非所有libstdc ++頭文件都在該路徑下,其他地方有一些基本頭文件,用於定義_GLIBCXX_BEGIN_NAMESPACE_VERSION

它不會因GCC而失敗,因為GCC已經知道如何找到其他頭文件,因此明確使用-I-L選項是多余的。 它不適用於Clang,因為你只是告訴它如何找到它需要的一些頭文件,但沒有告訴它如何找到其余的頭文件。

停止嘗試覆蓋編譯器的標准庫路徑,讓它使用它已經知道的內置路徑。

我在FreeBSD 9.0 / AMD64上使用clang-3.1和gcc4.6 libstdc ++。 它適用於以下選項:

-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/opt/local/include/gcc47/c++ \
-I/opt/local/include/gcc47/c++/x86_64-apple-darwin11.3.0 \
-L/opt/local/lib/gcc47

您可以使用特殊選項-gcc-toolchain ,它在編譯clang時由--with-gcc-toolchain隱式設置。 當你想使用另一個GCC庫時,它比重新編譯clang容易一些:)

像那樣:

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

或者,在你的情況下(我知道它是4歲:) :)它似乎是

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

'toolchain'文件夾應包含'include'和'lib'文件夾。 編譯器和鏈接器都使用此選項。 注意: - --gcc-toolchain不是一個有效的選項,使用一個短划線作為前綴(即使llvm wiki另有說明 - 我在clang 3.8 trunk上檢查過它)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM