How to cope with non-gcc compatible code in OS X Y

2019-02-05 07:35发布

问题:

I maintain a mixed C and C++ command line program that needs to run on Linux, Windows, and OS X. I recently upgraded to Yosemite and my OS X build is now failing. The error is:

/usr/include/dispatch/object.h:143:15: error: expected identifier or '(' before '^' token

Other folks have run into this bug.

The line of code that fails is a typedef that uses '^' which is a non-standard extension providing support for closures.

The underlying problem seems to be that some Apple standard headers are starting to require Clang specific extensions. Unfortunately our program has a very deep set of dependencies, some of which won't compile under Clang. We've been using the GCC compilers installed via MacPorts. I have a workaround for now: changing the line in the object.h header to be GCC compatible. However, hacking up the include files under /usr/include sounds to me like asking for trouble.

Can any OS X/Clang gurus suggest more sustainable ways of coping with this problem? Does this limit the future usefulness of GCC on OS X?

回答1:

Just for future visitors, the following should get most headers working with a recent GCC version:

In dispatch/object.h change

typedef void (^dispatch_block_t)(void);

to

#ifdef __clang__
typedef void (^dispatch_block_t)(void);
#else
typedef void* dispatch_block_t;
#endif

and in Availability.h change

#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED)

to

#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && defined(__clang__)