在mac上使用c++ 11和gcc 4.6.1
C++11 with gcc 4.6.1 on a mac
我是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版本。
相关文章:
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 奇怪的结构&GCC&clang(void*返回类型)
- GCC本机矩阵运算库
- PowerPC ppc64le上的Gcc Woverloaded虚拟错误
- gcc和c++17的过载解析失败
- 数据成员SFINAE的C++17测试:gcc vs clang
- GCC对可能有效的代码抛出init list生存期警告
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 使用gcc从静态链接的文件中查找可选符号
- 普通环路未使用gcc 4.8.5自动矢量化
- 有了gcc,是否可以链接库,但前提是它存在
- 在clang++预处理器中确定gcc工具链版本
- 为什么 gcc 编译这个而 msvc 没有
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 我可以检测和更改 gcc/g++ 中结构的当前数据对齐设置吗?
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 如何使用Clang/GCC在Mac上为C/C++设置VSCode
- 一位朋友将模板函数缩写为clang和gcc