Can an intrinsic compiler function be weakly defined or does Clang not respect weak attributes?

  c++, clang, gcc

Say there’s a function _foo which:

  • Is intrinsic to the Clang version I’m using, and
  • Is is not defined in the GCC version.

I’d like to have my code compile for both Clang and GCC and various versions.

I would expect that:

__attribute__((weak)) _foo (...) { my_foo(...); }

To allow me to use the Clang version if compiling with Clang, but use my_foo if using a compiler that does not have it as an intrinsic (e.g. my current GCC version).

The problem is that Clang thinks I’m redefining _foo.

I could ifdef off of __ clang __ but my concern is that for GCC versions that do have _foo as an intrinsic, compilation will break due to the same redefinition error.

So the question is:

  • Does the weak attribute not work with intrinsic functions? or
  • Clang does not respect the weak attribute?

Thanks in advance.

Source: Windows Questions C++

LEAVE A COMMENT