Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More than one __attribute__ in C with gcc

Tags:

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");   }   
like image 878
rubixibuc Avatar asked Apr 02 '11 21:04

rubixibuc


People also ask

What is __ attribute __ in C?

The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.

Which is a collection of the attributes of a variable?

A domain is a set of all possible values that a variable is allowed to have. The values are ordered in a logical way and must be defined for each variable. Domains can be bigger or smaller. The smallest possible domains have those variables that can only have two values, also called binary (or dichotomous) variables.

What is attribute in embedded C?

Master C and Embedded C Programming- Learn as you go Attributes are modern ways in C++ to standardize things if their code runs on different compilers. Attributes are used to provide some extra information that is used to enforce conditions (constraints), optimization and do specific code generation if required.

What are attributes in C programming?

Attributes are a mechanism by which the developer can attach extra information to language entities with a generalized syntax, instead of introducing new syntactic constructs or keywords for each feature.


2 Answers

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 version 4.4.3.

like image 79
David Grayson Avatar answered Nov 03 '22 00:11

David Grayson


You can use multiple __attribute__ specifiers separated by spaces.

char s[3] __attribute__((aligned(32))) __attribute__((weak)); 
like image 25
Heatsink Avatar answered Nov 02 '22 23:11

Heatsink