我想更改main,以便在执行任何其他操作之前调用一个函数。所以我写了这样的代码
#include <stdio.h>
void test()
{
printf("\nTEST\n");
#undef main
main(1,NULL);
}
int main(int argc, char** argv)
{
printf("\nHello World\n");
}
并将其编译为
cc -g -Dmain=test test.c -o test
但仍打印“Hello World”而非“TEST”。 我应该怎么做才能在主要做其他事情之前调用测试?
由于
答案 0 :(得分:3)
如果你想调用其他函数,在main之前,gcc提供__attribute__
例如:
int test(void) __attribute__ ((constructor));
int test()
{
printf("\nTEST\n");
return 0;
}
int main(int argc, char** argv)
{
printf("\nHello World\n");
return 0;
}