LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   #pragma startup directive goes unrecognized (https://www.linuxquestions.org/questions/programming-9/pragma-startup-directive-goes-unrecognized-598638/)

pratyush 11-10-2007 02:56 PM

HELP :- #pragma startup directive goes unrecognized
 
Hi

I am using Red Hat 11 Linux as my operating system.

I have written a simple program in C. I want to call a function before the main function gets called. For doing the same, I am using


#pragma startup.


My code is as follows:

#include <stdio.h>

void func();

#pragma startup func 0

int main()
{
printf("\nInside main\n");
return 0;
}

void func()
{
printf("\nInside func()\n");
}


But when I try to compile the code, the compiler is not able to recognize the pragma startup directive.
> gcc -Wall testPragma.c

testPragma.c:5: warning: ignoring #pragma startup func

Could anyone please help me in compiling the code so that I can use the pragma startup directive in my code?

Thanks and regards
Pratyush

Mara 11-10-2007 03:22 PM

As far as I know, gcc simply doesn't support the startup pragma.

pratyush 11-10-2007 03:31 PM

Then, does that mean that I have no way of calling any function before my main function gets called?

Phoxis 04-22-2012 03:07 AM

Check out the
Code:

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


constructs in gcc. To execute a function prior to main you need to do:

Code:

#include <stdio.h>

void begin (void) __attribute__((constructor));
void end (void) __attribute__((destructor));

int main (void)
{
  printf ("\nInside main ()");
}

void begin (void)
{
  printf ("\nIn begin ()");
}

void end (void)
{
  printf ("\nIn end ()\n");
}

Have a look here: http://phoxis.org/2011/04/27/c-langu...tors-with-gcc/


All times are GMT -5. The time now is 03:43 AM.