Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 with gcc 4.6.1 on a mac

Tags:

c++

macos

gcc

c++11

I'm new to the mac and trying to get gcc 4.6 working.

I installed MacPorts and installed gcc 4.6.1 (by executing 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'm getting errors that make me thing there is something wrong with the installed libraries.

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

The 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]

Note that if I use my dummy async function it compiles and runs fine.

I'm kind of stuck, do I have to install a specific library (version)? How do I do that?

like image 708
Fozi Avatar asked Aug 24 '11 15:08

Fozi


People also ask

Does my compiler support C ++ 11?

To see if your compiler has C++11 support, run it with just the --version option to get a print out of the version number. Do this for whichever compiler(s) you wish to use with Rosetta. Acceptable versions: GCC/g++: Version 4.8 or later.

Does G ++ support C ++ 11?

The g++ utility supports almost all mainstream C++ standards, including c++98 , c++03 , c++11 , c++14 , c++17 , and experimentally c++20 and c++23 . It also provides some GNU extensions to the standard to enable more useful features.

What C++ version does GCC support?

Master C and Embedded C Programming- Learn as you goC++98 − GCC has full support for the 1998 C++ standard as modified in 2003 and renamed to C++03 and some later defect reports. C++11 − GCC 4.8.


1 Answers

I've had similar issues with gcc-4.6.1 and OS X 10.6. The problem is C++0x's thread is not supported at the moment on OS X.

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

If you look in "${prefix}/include/c++/4.6.1/future" header file, you'll see the line:

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

Unfortunately, _GLIBCXX_HAS_GTHREADS evalute to 0 on OS X.

like image 97
fsaintjacques Avatar answered Sep 24 '22 22:09

fsaintjacques