Is it dependent on pragma directives?

I know, and I used to use #pragma startup and #pragma exit , but when I execute the following code, it only outputs In main . Can someone tell me what is going on here?

 #include<stdio.h> #pragma startup A 110 #pragma startup B #pragma exit A #pragma exit B 110 int main() { printf("\nIn main"); return 0; } void A() { printf("\nIn A"); } void B() { printf("\nIn B"); } 

Or does it depend on the compiler? I am using the gcc compiler.

+6
source share
4 answers

All #pragma are dependent on the compiler, and the compiler must ignore any that it does not recognize (ISO-9899: 2011, s6.10.6: "Any such pragma that is not recognized by the implementation is ignored."). This is why your program compiles successfully.

Functions A and B not called because ... you do not call them. I apologize if you understand this very well, but: the C program is executed by calling the main function. If you want functions A and B called, you must do this in the main function.

(In fact, the latest versions of the C standard introduced a small number of STDC STDC , the implementation of which must be recognized, but this does not have a significant impact on the answer)

+5
source

Yes, the #pragma is compiler dependent.

In particular, the supported parameters are specific to the compiler. Some parameters may be supported by many or most compilers, but in many cases the parameters are specific to each compiler.

+2
source

As far as I know, gcc just does not support the start / exit pragma. You will need to use the attribute so that it works with gcc.

 __attribute__((constructor)) __attribute__((destructor)) __attribute__((constructor (PRIORITY))) __attribute__((destructor (PRIORITY))) 

This will work:

  #include<stdio.h> void A() __attribute__((constructor(110))); void B() __attribute__((constructor)); void A() __attribute__((destructor)); void B() __attribute__((destructor(110))); int main() { printf("\nIn main"); return 0; } void A() { printf("\nIn A"); } void B() { printf("\nIn B"); } 
+1
source

All #pragma are implementation-defined. At one time, gcc responded to any and all #pragma in the same (usually undesirable) way.

0
source

All Articles