在 2026-6-10 23:33, Luca Bacci 写道:
Thanks for the insights!

 > There's possibility that some of them might not be inlined

This brings us another issue however: functions defined in headers should be 'static always_inline'. Why? If the compiler decides to emit function calls (say because code takes the address of the functions) we can introduce ODR violations. This happens when object files (or static libraries) are compiled with different compiler arguments, or if object files (or static libraries) are compiled with a previous version of mingw-w64 headers.

In C, if a function is defined `static inline` then it will not be callable from a user-defined inline function with external linkage:

   static inline
   int static_inline_fn(int x, int y) { return x + y; }

   // in C99 this is same as `extern inline __attribute__((__gnu_inline__))`.
   inline
   int plain_inline_fn(int x) { return static_inline_fn(x, x + 1); }


Per standard C (ISO/IEC 9899:2024 N3220):

   6.7.5 Function specifiers
   Constraints
   3 An inline definition of a function with external linkage shall not
     contain, ... and shall not contain, anywhere in the tokens making up the
     function definition, a reference to an identifier with internal linkage.


The code above is a constraint violation which results in undefined behavior.

C++ doesn't have this limitation.


Now, it occurs to me that there may be another reason for library functions. Code targeting MSVC can use intrinsics even without including intrin.h:

source.c:
------------
long _InterlockedExchange(long volatile* _Target, long _Value);
#pragma intrinsic(_InterlockedExchange)

int main(void)
{
     long value = 0;
     return _InterlockedExchange(&value, 1);
}

This also works on GCC, probably thanks to library implementations in crt.o.

Is there a reason why someone wants to do that? Why would someone want to declare a function whose name is known to be reserved and compiler-specific?


--
Best regards,
LIU Hao

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to