在mac上使用gcc 4.6.1的C++ 11

Foz*_*ozi 6 c++ macos gcc c++11

我是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;
}
Run Code Online (Sandbox Code Playgroud)

错误消息:

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]
Run Code Online (Sandbox Code Playgroud)

请注意,如果我使用我的虚拟异步函数,它编译并运行正常.

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

fsa*_*ues 5

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

看到这篇文章:c ++ 0x,std :: thread错误(线程不是std的成员)

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

#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
        && defined(_GLIBCXX_ATOMIC_BUILTINS_4)
Run Code Online (Sandbox Code Playgroud)

不幸的是,_GLIBCXX_HAS_GTHREADS在OS X上评估为0.


Foz*_*ozi 3

我们快到了

gcc 4.7(端口)可以很好地编译此代码。

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

而这仅仅用了半年的时间。