【问题标题】:C++11 with gcc 4.6.1 on a macMac 上的 C++11 和 gcc 4.6.1
【发布时间】:2011-11-02 22:07:12
【问题描述】:

我是 mac 新手,正在尝试让 gcc 4.6 正常工作。

我安装了 MacPorts 并安装了 gcc 4.6.1(通过执行 sudo port install gcc46)。我正在尝试编译一个简单的测试代码,它可以在 Linux(使用 gcc 4.6.1 和 4.6.2)和 Windows 上正常编译,但我遇到的错误让我觉得安装的库有问题。

#include <iostream>
#include <type_traits>
#include <future>

struct test {
    void get() {}
};

/*template<typename Func>
test async(const Func &f) {
    f();
    return test();
}*/

using namespace std;

int main (int argc, const char * argv[])
{
    auto t1 = async([]() -> int{
        cout << "This is thread 1" << endl;
        return 1;
    });

    auto t2 = async([]() -> int {
        cout << "This is thread 2" << endl;
        return 2;
    });

    std::cout << "This is the main thread" << endl;

    t1.get();
    t2.get();

    return 0;
}

错误信息:

macbook01:Test fozi$ g++ main.cpp -o test -std=c++0x
main.cpp: In function 'int main(int, const char**)':
main.cpp:30:6: error: invalid use of incomplete type 'std::enable_if<true, std::future<int> >::type'
/opt/local/include/gcc46/c++/future:111:11: error: declaration of 'std::enable_if<true, std::future<int> >::type'
main.cpp:30:6: error: unable to deduce 'auto' from '<expression error>'
main.cpp:35:6: error: invalid use of incomplete type 'std::enable_if<true, std::future<int> >::type'
/opt/local/include/gcc46/c++/future:111:11: error: declaration of 'std::enable_if<true, std::future<int> >::type'
main.cpp:35:6: error: unable to deduce 'auto' from '<expression error>'
/opt/local/include/gcc46/c++/future: At global scope:
/opt/local/include/gcc46/c++/future:150:5: error: 'typename std::enable_if<(! std::is_same<typename std::decay<_Functor>::type, std::launch>::value), std::future<decltype (declval<_Fn>()((declval<_Args>)()...))> >::type std::async(_Fn&&, _Args&& ...) [with _Fn = main(int, const char**)::<lambda()>, _Args = {}, typename std::enable_if<(! std::is_same<typename std::decay<_Functor>::type, std::launch>::value), std::future<decltype (declval<_Fn>()((declval<_Args>)()...))> >::type = std::future<int>]', declared using local type 'main(int, const char**)::<lambda()>', is used but never defined [-fpermissive]
/opt/local/include/gcc46/c++/future:150:5: error: 'typename std::enable_if<(! std::is_same<typename std::decay<_Functor>::type, std::launch>::value), std::future<decltype (declval<_Fn>()((declval<_Args>)()...))> >::type std::async(_Fn&&, _Args&& ...) [with _Fn = main(int, const char**)::<lambda()>, _Args = {}, typename std::enable_if<(! std::is_same<typename std::decay<_Functor>::type, std::launch>::value), std::future<decltype (declval<_Fn>()((declval<_Args>)()...))> >::type = std::future<int>]', declared using local type 'main(int, const char**)::<lambda()>', is used but never defined [-fpermissive]

请注意,如果我使用我的虚拟异步函数,它会编译并运行良好。

我有点卡住了,我必须安装特定的库(版本)吗?我该怎么做?

【问题讨论】:

  • 嗯...你确定你从正确的路径中获取标题吗?您可以使用-H 运行 gcc 以查看标头的来源。我之前在 MacOS 上从源代码构建了 GCC 4.6.0,没有任何问题。
  • 我实际上试图修复它们并看到了变化,所以是的,我很确定我使用了正确的库。
  • 你有一个奇怪的future 包含文件。在我的第 111 行和第 150 行似乎与std::enable_if 无关(我也有 4.6.1)。也许它已损坏。
  • 你还在用g++命令吗?在这种情况下,它可能没有被更换。尝试 g++ --version 以获取您正在使用的实际版本。此外,如果您只获得编译器,则没有任何说明您也拥有这些库......或者您可能必须单独包含它们(尝试 -v 标志以查看包含头文件路径)
  • Mac 没有未来,j/k ... 我认为 MacPorts GCC 对 C++0x 有一些问题 Apple GCC 仍然不支持 lambdas 吗?

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


【解决方案1】:

尝试g++ -v 获取您正在使用的 G++ 版本。我没有使用预编译的编译器,但也许对你来说是一样的。

GCC 4.6.0 安装在/usr/local/bin 中,名称为x86_64-apple-darwin10.7.0-g++

如果你仍然使用简单的 g++ 命令,它可能仍然是/usr/bin/g++,这是苹果的 llvm-gcc 风格。

【讨论】:

  • 想发表评论,对不起:s
  • 我正在从终端启动 g++,所以是的,我确定版本是正确的。此外,我将原件重命名为 gcc-4.2 等等,并使用符号链接来切换它们。这包括 /Development 中的那些。
【解决方案2】:

我在 gcc-4.6.1 和 OS X 10.6 上遇到过类似的问题。问题是 OS X 目前不支持 C++0x 的线程。

看到这个帖子:c++0x, std::thread error (thread not member of std)

如果您查看“${prefix}/include/c++/4.6.1/future”头文件,您会看到以下行:

#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
        && defined(_GLIBCXX_ATOMIC_BUILTINS_4)

不幸的是,_GLIBCXX_HAS_GTHREADS 在 OS X 上计算为 0。

【讨论】:

    【解决方案3】:

    我们快到了

    gcc 4.7 (port) 可以很好地编译这段代码。

    Xcode 4.3 附带了 clang 3.1,它应该支持这个,但是当我尝试编译时它崩溃了。但是我从 SVN 构建了 clang 并替换了 Xcode 正在使用的编译器,现在它也可以编译和运行了。

    而且只用了半年。

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 2014-03-31
      • 2013-10-31
      • 1970-01-01
      相关资源
      最近更新 更多