rubixibuc
rubixibuc

Reputation: 7397

More than one __attribute__ in C with gcc

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");  
}  

Upvotes: 29

Views: 25589

Answers (2)

David Grayson
David Grayson

Reputation: 87386

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.

Upvotes: 49

Heatsink
Heatsink

Reputation: 7751

You can use multiple __attribute__ specifiers separated by spaces.

char s[3] __attribute__((aligned(32))) __attribute__((weak));

Upvotes: 11

Related Questions