Issue
Can you add more than one attribute to an identifier in C with gcc? Here is what I have now. I left out the include statements because they get scramble in the post. If there is a way to add two, what is the general syntax, and how can I do it both with the defintion, and with a prototype? Thank you. :-)
main() {
printf("In Main\n");
}
__attribute__ ((constructor)) void beforeMain(void)
{
printf("Before Main\n");
}
Solution
There are two different ways of specifying multiple attributes in C with GCC:
#include <stdio.h>
// Attributes in prototypes:
__attribute__((constructor, weak)) void beforeMain(void);
__attribute__((constructor)) __attribute__((weak)) void beforeMain2(void);
int main(){
printf("In Main\n");
return 0;
}
// Attributes in definitions:
__attribute__((constructor, weak)) void beforeMain(void){
printf("Before Main 1\n");
}
__attribute__((constructor)) __attribute__((weak)) void beforeMain2(void){
printf("Before Main 2\n");
}
The code above compiles and runs correctly for me under GCC versions 4.4.3 and 12.3.0.
Answered By - David Grayson Answer Checked By - Gilberto Lyons (WPSolving Admin)