What is the use of “#pragma section ” in C?

2019-04-21 20:25发布

What is the use of "#pragma section <XYZ>" in C ?

I have come across C code file where the following kind was used:-

#define XYZ "ITEM 26.G03"

#pragma section <XYZ>

where XYZ is: #define XYZ "ITEM 26.G03"

I need some explaination on the use of "#pragma section"

标签: c pragma
2条回答
在下西门庆
2楼-- · 2019-04-21 20:51

The #pragma directive is an implementation specific directive it is a standard way to provide additional information to the compiler. This directive has the following form:

#pragma name

If the preprocessor recognizes the specified "name", it performs whatever action they stand for, or passes information on to the compiler. If "name" is not supported by the c implementation it's ignored.

For example gcc compiler accept the list of pragmas listed here.

For the #pragma section, the documentation of gcc said:

section ("section-name") Normally, the compiler places the code it generates in the text section. Sometimes, however, you need additional sections, or you need certain particular functions to appear in special sections. The section attribute specifies that a function lives in a particular section. For example, the declaration:

      extern void foobar (void) __attribute__ ((section ("bar")));

puts the function foobar in the bar section.

Some file formats do not support arbitrary sections so the section attribute is not available on all platforms. If you need to map the entire contents of a module to a particular section, consider using the facilities of the linker instead.

More on that here.

查看更多
冷血范
3楼-- · 2019-04-21 20:52

Section creates a section in an .obj file.
Refer to MSDN for more details.

Code and data are generated in sections in an object file, combined by the linker into an executable file, and ultimately located in target memory at specific locations. Default sections are predefined and have certain attributes. The section pragmas may be used to change the default attributes, to define new sections, and to control the assignment of code and variables to particular sections and, along with the linker command file, their locations.

#pragma section defines a section class, and optionally, one or two sections in the class. A section class controls the addressing and accessibility of variables and code placed in an instance of the class.

查看更多
登录 后发表回答