【发布时间】: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 吗?