How widely supported is weak pragma and overcomes the problems of using the gcc attribute?

I just opened the #pragma weak in GCC:

6.57.9 Weak Pragmatists

For compatibility with SVR4, GCC supports the #pragma set of directives for declaring weak characters and defining weak aliases.

#pragma weak symbol

This pragma declares the character weak, as if the ad has an attribute with the same name. A pragma may appear before or after the declaration of a symbol. This is not a mistake for a character that can never be defined at all.

#pragma weak symbol1 = symbol2

This pragma declares character1 a weak alias for character2. This is an error if character2 is not defined in the current translation block.

http://gcc.gnu.org/onlinedocs/gcc/Weak-Pragmas.html

Although GCC developers generally dislike #pragma and are encouraged to use __attribute__ instead for all sorts of things that might be pragmas, I tend to believe that #pragma weak may actually be superior to an attribute-based approach that looks So:

 extern __typeof(old_name) new_name __attribute__(weak, alias("old_name")) 

Besides the ugliness requiring __typeof (or requiring you to clearly know the type and spell, even if it is really a complex type of function), the biggest problem with the attribute based approach is that "old_name" should be passed to gcc as a string for inserts literally into the generated assembly. This is problematic because different systems have different characteristics for changing the name (the most popular is the underscore prefix for all C character names or does nothing at all) and pass the correct string to the alias attribute, you need to know which one you are creating, which is not really knowledge , which belongs to the application-level library, where weak aliases can be useful.

The syntax #pragma weak new_name = old_name seems to avoid this problem by handling both names at the compiler level, where t can mutilate them properly if I am not mistaken.

So, with all the preliminary results, my current questions are: Am I mistaken about #pragma weak that has this "portability" advantage? and Do all modern compilers on Unix-like systems (gcc, pcc, tinycc, icc, llvm / clang, etc.) still support the traditional SVR4 #pragma weak ?

I know the following similar question, but this is not like that, and the answers to not satisfactorily affect my question:

How tolerable is weak communication? #pragma weak my_symbol

+6
source share
1 answer

Neither "# pragma is weak" nor __attribute__ are part of the C standard, so none of them, strictly speaking, are portable. Some C compilers tend to be compatible with most GCC extensions to the C standard, while others do not.

Generally speaking, if you are already at the level of talking about weak characters and weak aliases, you probably have passed the place where you can write code that is reliably ported through compilers. Even your toolchain will become a problem here (including, in particular, the linker) - I don’t think you can rely on anything without thorough testing.

Edited to add: The original poster, commented below, asks if #pragma is pragmatic than __attribute __.

My own experience was this: it’s nice to be able to hide all such things inside macros or other generated code in order to simplify portability. __attribute__ is easier to hide inside the portability header file. For example, at least one of the BSD kernels has cdefs.h, which uses __attribute__ inside a macro to centralize the way weak definitions are executed throughout the code base to facilitate changes to new compilers. #pragma is harder to use this way. Such a macro can also hide the difference between different names using the CPP gluing operator ("##", etc.)

For an example of this use, see: http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/sys/cdefs.h?rev=1.89.6.1&content-type=text/x-cvsweb-markup

+4
source

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


All Articles