在mac上使用c++ 11和gcc 4.6.1

C++11 with gcc 4.6.1 on a mac

本文关键字:gcc c++ mac      更新时间:2023-10-16

我是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]

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

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

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

查看这个帖子:c++0x, std::thread error(线程不是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。

我们快到了

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

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

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

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

如果您仍然使用简单的g++命令,它可能仍然是/usr/bin/g++,这是Apple的llvm-gcc版本。