这里我使用了#pragma指令,它用于在C程序中的main函数之前和之后调用函数。 所以,我的预期输出是--->的您好 你好 BYE
但是当执行这段代码时,我得到的输出为---> 你好
#include <stdio.h>
#pragma startup fun1
#pragma exit fun2
void fun1();
void fun2();
int main()
{
printf("\nHELLO");
return 0;
}
void fun1()
{
printf("\nHi");
}
void fun2()
{
printf("\nBYE");
}
答案 0 :(得分:0)
来自GCC的onlinedocs,7 Pragmas。
'#pragma'指令是C标准指定的方法 除了什么之外,还为编译器提供了额外的信息 用语言本身传达。这个指令的形式(通常 由C标准指定的称为pragma的前缀为STDC。一个C. 编译器可以自由地将它喜欢的任何含义附加到其他编译指示。所有 GNU定义的,支持的pragma已被赋予GCC前缀。
也就是说,GCC不支持startup
和exit
个pragma。
GCC确实支持使用attributes来创建类似的行为。特别是:
__attribute__((constructor))
__attribute__((destructor))
__attribute__((constructor (PRIORITY)))
__attribute__((destructor (PRIORITY)))
对于你的例子:
#include <stdio.h>
void fun1() __attribute__((constructor));
void fun2() __attribute__((destructor));
int main (void){
printf ("\nHELLO");
}
void fun1 (){
printf ("\nHI");
}
void fun2 (){
printf ("\nBYE\n");
}