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

user1074836 picture user1074836 · Aug 16, 2012 · Viewed 22.4k times · Source

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"

Answer

TOC picture TOC · Aug 16, 2012

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.