//aligned.c
#include
#include
#ifdef ON
//关键字__attribute__也可以对结构体(struct)或共用体(union)进行属性设置。大致有六个参数值可以被设定,
//即:aligned, packed, transparent_union, unused, deprecated 和 may_alias。
struct p
{
int a;
char b;
char c;
}__attribute__((aligned(4))) pp;
struct q
{
int a;
char b;
struct p qn;
char c;
}__attribute__((aligned(8))) qq;
#else
struct p
{
int a;
char b;
char c;
} pp;
struct q
{
int a;
char b;
struct p qn;
char c;
} qq;
#endif
//__attribute__修饰变量属性(Variable Attribute)
struct test
{
char a;
int x[2] __attribute__ ((packed));
};
//gcc -Wall aligned.c -D ON -o a2
//gcc -Wall aligned.c -o a1
int main()
{
printf("sizeof(int)=%d,sizeof(short)=%d.sizeof(char)=%d\n",sizeof(int),sizeof(short),sizeof(char));
printf("pp=%d,qq=%d \n", sizeof(pp),sizeof(qq));
return 0;
}
//const.c
#include
#include
#ifdef ON
extern int square(int n) __attribute__((const));
#else
extern int square(int n);
#endif
//编译对比
//gcc -Wall const.c -D ON
//gcc -Wall const.c
int main()
{
int total=0;
int i=0;
for (i = 0; i < 100; i++ )
{
total += square(5) + i;
}
printf("total=%d\n",total);
return 0;
}
//constructor_destructor.c
#include
#include
#ifdef ON
static void start(void) __attribute__ ((constructor));
static void stop(void) __attribute__ ((destructor));
#else
static void start(void);
static void stop(void);
#endif
void start(void)
{
printf("hello world!\n");
}
void stop(void)
{
printf("goodbye world!\n");
}
//编译运行对比
//gcc -Wall noreturn.c -D ON
//gcc -Wall noreturn.c
int main(int argc, char *argv[])
{
printf("start == %p\n", start);
printf("stop == %p\n", stop);
return 0;
}
//format-myprintf.c
#include
#include
#ifdef ON
//告诉编译器以printf的方式去检查该函数
extern void myprint(const char *format,...) __attribute__((format(printf,1,2)));
#else
extern void myprint(const char *format,...);
#endif
void test()
{
myprint("i=%d\n",6);
myprint("i=%s\n",6);
myprint("i=%s\n","abc");
myprint("%s,%d,%d\n",1,2);
}
//gcc -Wall format-myprintf.c -D ON
//gcc -Wall format-myprintf.c
int main()
{
test();
return 0;
}
//noreturn.c
#include
#include
#ifdef ON
extern void myexit() __attribute__((noreturn));
#else
extern void myexit() ;
#endif
int test(int n)
{
if ( n > 0 )
{
myexit();
/* 程序不可能到达这里*/
}
else
return 0;
}
//https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html
//__attribute__ 告诉编译器做额外的事情。
//编译对比
//gcc -Wall noreturn.c -D ON
//gcc -Wall noreturn.c
int main()
{
test(3);
return 0;
}
#include
#include
#ifdef ON
extern "C" int
pthread_create (pthread_t*,
const pthread_attr_t*,
void* (*)(void*),
void*) __attribute__ ((weak));
extern "C" int
pthread_mutex_init (pthread_mutex_t*,
const pthread_mutexattr_t*) __attribute__ ((weak));
extern "C" int
pthread_mutex_lock (pthread_mutex_t*) __attribute__ ((weak));
extern "C" int
pthread_mutex_unlock (pthread_mutex_t*) __attribute__ ((weak));
extern "C" int
pthread_mutex_destroy (pthread_mutex_t*) __attribute__ ((weak));
#else
extern "C" int
pthread_create (pthread_t*,
const pthread_attr_t*,
void* (*)(void*),
void*);
extern "C" int
pthread_mutex_init (pthread_mutex_t*,
const pthread_mutexattr_t*);
extern "C" int
pthread_mutex_lock (pthread_mutex_t*);
extern "C" int
pthread_mutex_unlock (pthread_mutex_t*);
extern "C" int
pthread_mutex_destroy (pthread_mutex_t*);
#endif
#define __init __attribute__ ((__section__ (".text.init")))
#define __exit __attribute__ ((unused, __section__(".text.exit")))
#define __initdata __attribute__ ((__section__ (".data.init")))
#define __exitdata __attribute__ ((unused, __section__ (".data.exit")))
#define __initsetup __attribute__ ((unused,__section__ (".setup.init")))
#define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
#define __exit_call __attribute__ ((unused,__section__ (".exitcall.exit")))
extern void foobar (void) __attribute__ ((section ("bar")));
void add(int a,int b) __attribute__ ((visibility ("protected")));
void sub(int a,int b) __attribute__ ((visibility ("hidden")));
//关键字__attribute__ 也可以对结构体(struct )或共用体(union )进行属性设置。大致有六个参数值可以被设定,
//即:aligned, packed, transparent_union, unused, deprecated 和 may_alias 。
struct STudent{
int age;
char name[10] __attribute__ ((aligned(8)));
};
//https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html#Function-Attributes
int old_fn () __attribute__ ((deprecated));
void f () __attribute__ ((weak, alias ("__f")));
void mul() __attribute__ ((dllexport));
void mul2() __attribute__ ((dllimport));
extern char * my_dgettext (char *my_domain, const char *my_format) __attribute__ ((format_arg (2)));
void f2 () __attribute__ ((interrupt ("IRQ")));
//Use naked attribute on the ARM, AVR, C4x and IP2K ports to indicate that the specified function does not need prologue/
//epilogue sequences generated by the compiler.It is up to the programmer to provide these sequences.
void mydiv() __attribute__ ((naked));
void mydiv2() __attribute__ ((noinline));
__attribute__ ((noinline,__annotate__(("nofcf"))))
__attribute__ ((noinline,__annotate__(("nocxf"))))
__attribute__ ((noinline,__annotate__(("nosac")))) void demo();
//编译对比
//gcc -Wall weak.c -D ON
//gcc -Wall weak.c
int main()
{
return 0;
}