Please note as of Wednesday, August 15th, 2018 this wiki has been set to read only. If you are a TI Employee and require Edit ability please contact x0211426 from the company directory.

Pragmas in C++

From Texas Instruments Wiki
Jump to: navigation, search

The Big Difference vs C

In C, pragmas specifically name the entity they operate on, and thus can appear some distance away from where that entity is defined. For example ...

#pragma CODE_SECTION(fxn, "mysect");
 
// many lines later ...
 
void fxn(int arg) { ...      // fxn is finally defined

In C++, the pragmas are positional. They do not name the entity they operate on. They always operate on the next entity defined after the pragma. The above example, in C++, must be written ...

#pragma CODE_SECTION("mysect")  // NO semi-colon!
// The affected function must be the very next entity
void fxn(int arg) { ...

Hide It in a Macro

If you need your code to work whether it is compiled as either C or C++ code, a macro like this is useful.

#define PRAGMA(x) _Pragma(#x)
#ifdef __cplusplus
#define CSPRAGMA(f,s) PRAGMA(CODE_SECTION(s))
#else
#define CSPRAGMA(f,s) PRAGMA(CODE_SECTION(f, s))
#endif
 
CSPRAGMA(fxn, "mysect")  // NO semi-colon
void fxn(int arg) { ...

This macro is, of course, positional when used in C++. So, for the code to be portable, the macro must be viewed as positional even when used in C code.

Pragmas You Can Understand is a good article on wrapping pragmas in a macro, via _Pragma, for better portability.