C ++ 11 with gcc 4.6.1 on mac

I am new to Mac and trying to run gcc 4.6.

I installed MacPorts and installed gcc 4.6.1 (by running 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 get errors that make me say that something is wrong with the libraries installed.

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

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] 

Please note that if I use my dummy asynchronous function, it compiles and works fine.

I'm kinda stuck, do I need to install a specific library (version)? How to do it?

+6
source share
3 answers

We get there

gcc 4.7 (port) compiles this code just fine.

Xcode 4.3 comes with clang 3.1, which should support this, but it crashes when I try to compile. However, I created clang from SVN and replaced the compiler that uses Xcode, and now it compiles and works fine.

And it took six months.

+3
source

I had similar problems with gcc-4.6.1 and OS X 10.6. The problem is that the C ++ 0x stream is currently not supported on OS X.

See this post: C ++ 0x, std :: thread error (thread not member of std)

If you look in the header file $ {prefix} / include / C ++ / 4.6.1 / future, you will see the line:

 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \ && defined(_GLIBCXX_ATOMIC_BUILTINS_4) 

Unfortunately, _GLIBCXX_HAS_GTHREADS checks the value 0 on OS X.

+5
source

Try g++ -v to get the version of g ++ that you are using. I do not use a precompiled compiler, but maybe this is the same for you.

GCC 4.6.0 is installed in /usr/local/bin as x86_64-apple-darwin10.7.0-g++

If you are still using the simple g ++ command, it could be /usr/bin/g++ , which matches the taste of Apple llvm-gcc.

0
source

Source: https://habr.com/ru/post/895764/


All Articles