Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pragma in define macro

Is there some way to embed pragma statement in macro with other statements?

I am trying to achieve something like:

#define DEFINE_DELETE_OBJECT(type)                      \     void delete_ ## type_(int handle);                  \     void delete_ ## type(int handle);                                                \     #pragma weak delete_ ## type_ = delete_ ## type 

I am okay with boost solutions (save for wave) if one exists.

like image 959
Anycorn Avatar asked Jun 12 '10 21:06

Anycorn


People also ask

How is pragma defined?

A pragma is a compiler directive that allows you to provide additional information to the compiler. This information can change compilation details that are not otherwise under your control. For example, the pack pragma affects the layout of data within a structure. Compiler pragmas are also called directives.

What is pragma command?

The pragma command is specific to SQLite and is not compatible with any other SQL database engine. Specific pragma statements may be removed and others added in future releases of SQLite. There is no guarantee of backwards compatibility. No error messages are generated if an unknown pragma is issued.

What is pragma statement in C?

The #pragma in C is a directive that is provided by the C standard in order to provide extra required details to the C compiler. These extra details can be anything that was somehow not passed within the program or the code logic. These directives, known as pragma are prefixed by the STDC in the standard.

What is #pragma message?

A typical use of the message pragma is to display informational messages at compile time. The message-string parameter can be a macro that expands to a string literal, and you can concatenate such macros with string literals in any combination.


2 Answers

If you're using c99 or c++0x there is the pragma operator, used as

_Pragma("argument") 

which is equivalent to

#pragma argument 

except it can be used in macros (see section 6.10.9 of the c99 standard, or 16.9 of the c++0x final committee draft)

For example,

#define STRINGIFY(a) #a #define DEFINE_DELETE_OBJECT(type)                      \     void delete_ ## type ## _(int handle);                  \     void delete_ ## type(int handle);                   \     _Pragma( STRINGIFY( weak delete_ ## type ## _ = delete_ ## type) ) DEFINE_DELETE_OBJECT(foo); 

when put into gcc -E gives

void delete_foo_(int handle); void delete_foo(int handle); #pragma weak delete_foo_ = delete_foo  ; 
like image 106
Scott Wales Avatar answered Sep 17 '22 22:09

Scott Wales


One nice thing you can do with _Pragma("argument") is use it to deal with some compiler issues such as

#ifdef _MSC_VER #define DUMMY_PRAGMA _Pragma("argument") #else #define DUMMY_PRAGMA _Pragma("alt argument") #endif 
like image 44
John Thomas Avatar answered Sep 20 '22 22:09

John Thomas