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

时间:2012-04-25 01:22:55

标签: c++ macos gcc c++11 clang

我一直在努力让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兼容性,由此线程表示:Why can't clang with libc++ in c++0x mode link this boost::program_options example?

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

#include <mutex>
#include <thread>

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

我希望include应该在c ++ 11下工作。

所以这是我如何编译它: 与海湾合作委员会

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 ++的一个铿锵问题?

3 个答案:

答案 0 :(得分:8)

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

对于GCC或Clang,这不应该是必要的,并且不起作用。并非所有libstdc ++标头都在该路径下,其他地方有一些基本标题可以定义_GLIBCXX_BEGIN_NAMESPACE_VERSION

之类的内容。

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

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

答案 1 :(得分:7)

我在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

答案 2 :(得分:0)

您可以使用special option -gcc-toolchain--with-gcc-toolchain在编译clang时由~/clang/trunk/bin/clang++ main.cc -gcc-toolchain ~/gcc/trunk -o main 隐式设置。当你想使用另一个GCC库时,它比重新编译clang更容易:)

就像那样:

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

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

--gcc-toolchain

'toolchain'文件夹应包含'include'和'lib'文件夹。编译器和链接器都使用此选项。注意:IF OBJECT_ID('tempdb..#BudgetTransTmp') IS NOT NULL DROP TABLE #BudgetTransTmp Select Row_number() over(ORDER BY YEAR(BTLine.DATE),MONTH(BTLine.DATE)) as RowNumber, COMBINATION.DISPLAYVALUE, BTLine.LedgerDimension AS LedgerDimension, MIN(BTLine.TransactionCurrencyAmount) AS Amount, SUM(BTLine.TransactionCurrencyAmount) OVER (ORDER BY YEAR(BTLine.DATE),MONTH(BTLine.DATE),BTLine.LedgerDimension,COMBINATION.DISPLAYVALUE ) AS SUM, YEAR(BTLine.DATE) AS Year , MONTH(BTLine.DATE) AS MONTH INTO #BudgetTransTmp FROM MicrosoftDynamicsAX.dbo.BudgetTransactionLine AS BTLine --Get Display value INNER JOIN MicrosoftDynamicsAX.dbo.DIMENSIONATTRIBUTEVALUECOMBINATION AS COMBINATION ON COMBINATION.RECID = BTLine.LEDGERDIMENSION GROUP BY BTLine.LedgerDimension, YEAR(BTLine.DATE), MONTH(BTLine.DATE) ORDER BY RowNumber 不是有效选项,使用一个短划线作为前缀(即使llvm wiki另有说明 - 我在clang 3.8 trunk上检查过它。)

相关问题