Special attribute Mechanism

Source: Internet
Author: User

The _ attribute _ mechanism is a major feature of gnu c. It can be used to set attributes of functions, variables, and data types. Next, we will give a brief introduction to the two attributes that are useful in our work.

This attribute specifies that the function is executed before the main function is executed.

  RunBeforeMain( RunBeforeMain(% main(% 

The output sequence is as follows:

RunbeforeMain

Main

 

2. weak, alias

Weak: weak symbol. if two identical global symbols exist, a redefinition error is thrown. if weak attribute is used, when both weak symbol and non-weak symbol exist, linker uses non-weak symbol. if only weak symbol exists, only weak symbol is used.

Alias: sets the alias of a function, as shown in figure

 example() __attribute__((alias()));

Set the example function as the alias of _ example.

These two attributes can be used together to compile and run successfully even if the non-weak symbol is not declared.

// Strong. c

#include <stdio.h>

// Weak. c

#include <stdio.h> StrongFun()__attribute__((weak, alias( main( argc,  ** 

For separate links,

Gcc-o weak. o

./Weak

Output result:

Weak. c: WeakFun

At the same time,

Gcc-o strong weak. o strong. o

./Strong

Output result

Strong. c: StrongFun

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.