在mac上使用gcc 4.6.1编译C++11

6
我刚开始接触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]

请注意,如果我使用虚拟异步函数,它可以正常编译和运行。我有些困惑,我需要安装特定的库(版本)吗?我该怎么做?

你还在使用g++命令吗?如果是这种情况,它可能还没有被替换。尝试使用g++ --version获取您正在使用的实际版本。 此外,如果您只获得编译器,则不表示您也有库...或者您可能需要单独包含它们(尝试-v标志以查看包含头文件路径)。 - Geoffroy
Mac并没有没有未来,开个玩笑...我觉得MacPorts GCC在C++0x方面存在一些问题,苹果GCC仍然不支持lambda表达式吗? - AJG85
此外,MacPorts的gcc46实际上是4.6.0而不是4.6.1(我认为它实际上是一个发布候选版本,发布日期为25/03/11)。 - mmmmmm
@Mark 可以解释一下文件的差异。我尝试将头文件更新到4.6.2,但也失败了。我没有成功在 Mac 上构建gcc 4.6.2。 - Fozi
4.6.2尚未发布,因此我建议您坚持使用4.6.1,除非您想调试编译器。 - mmmmmm
显示剩余6条评论
3个回答

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)

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


3

我们正在逐步接近目标

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

Xcode 4.3自带clang 3.1,应该支持它,但当我尝试编译时,它会崩溃。然而,我从SVN构建了clang并替换了Xcode正在使用的编译器,现在它也可以很好地编译和运行。

只花了半年的时间。


0

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

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

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


抱歉,我想将它作为评论发布,不好意思 :s - Geoffroy
我从终端开始使用g++,所以我确定版本是正确的。此外,我将原文件重命名为gcc-4.2等,并使用符号链接进行切换。这包括/Development中的文件。 - Fozi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接