本文介绍了C ++ 11与gcc 4.6.1在mac上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的mac,并试图获得gcc 4.6工作。



我安装了MacPorts并安装了gcc 4.6.1(通过执行 sudo port install gcc46 )。我试图编译一个简单的测试代码,在Linux(与gcc 4.6.1和4.6.2)和Windows编译良好,但我得到错误,使我的东西在安装的库有问题。 p>

  #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<<这是第2个线程 return 2;
});

std :: cout<< 这是主线程< endl

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

return 0;
}

错误讯息:

  macbook01:Test fozi $ g ++ main.cpp -o test -std = c ++ 0x 
main.cpp:在函数'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:'std :: enable_if< true,std :: future< int& > :: type'
main.cpp:30:6:错误:无法从'< expression error>'推导'auto'
main.cpp:35:6:error:invalid use不完全类型'std :: enable_if< true,std :: future< int> > :: type'
/ opt / local / include / gcc46 / c ++ / future:111:11:error:'std :: enable_if< true,std :: future< int& > :: type'
main.cpp:35:6:错误:无法从'< expression error>'推断'auto'
/ opt / local / include / gcc46 / c ++ / future :在全局范围:
/ 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>()(declave< _Args>)()...) > :: type std :: async(_Fn&&&&&&& ...)[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> (declat< _Args>)()...))> > :: type = std :: future< int>]',使用局部类型'main(int,const char **)::< lambda()
/ 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>()(declave< _Args>)()...) > :: type std :: async(_Fn&&&&&&& ...)[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> (declat< _Args>)()...))> > :: type = std :: future< int>]',使用局部类型'main(int,const char **)::< lambda()请注意,如果我使用我的虚拟异步函数,它会编译并运行良好。



$ b

$ b

我卡住了,我要安装一个特定的库(版本)吗?

解决方案

我们到那里



gcc 4.7(port)编译这段代码就好了。



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



这只需要半年时间。


I'm new to the mac and trying to get gcc 4.6 working.

I installed MacPorts and installed gcc 4.6.1 (by executing sudo port install gcc46). I'm trying to compile a simple test code that compiles fine on Linux (with gcc 4.6.1 and 4.6.2) and Windows, but I'm getting errors that make me thing there is something wrong with the installed libraries.

#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;
}

The error messages:

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]

Note that if I use my dummy async function it compiles and runs fine.

I'm kind of stuck, do I have to install a specific library (version)? How do I do that?

解决方案

We're getting there

gcc 4.7 (port) compiles this code just fine.

Xcode 4.3 comes with clang 3.1 which is supposed to support this, but it crashes when I try to compile. However I built clang from SVN and replaced the compiler Xcode is using and now it compiles and runs fine as well.

And it only took half a year.

这篇关于C ++ 11与gcc 4.6.1在mac上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:31