Pragma in macro definition

Is there a way to insert a pragma statement into a 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 fine with boosting solutions (with the exception of the wave), if one exists.

+74
c-preprocessor pragma stringification
Jun 12 '10 at 21:09
source share
4 answers

If you use c99 or C ++ 0x, there is a pragma operator used as

 _Pragma("argument") 

which is equivalent

 #pragma argument 

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

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 typed in gcc -E gives

 void delete_foo_(int handle); void delete_foo(int handle); #pragma weak delete_foo_ = delete_foo ; 
+92
Jun 12 2018-10-12T00:
source share

One nice thing you can do with _Pragma (the "argument") is using it to solve some problems with the compiler, such as

 #ifdef _MSC_VER #define DUMMY_PRAGMA _Pragma("argument") #else #define DUMMY_PRAGMA _Pragma("alt argument") #endif 
+4
Feb 28 '12 at 15:34
source share

No, there is no portable way to do this. Again, there are no portable ways to use #pragma. Because of this, many C / C ++ compilers define their own methods for creating pragma-like things, and they can often be embedded in macros, but for each compiler you need a different macro definition. If you want to go this route, you often do things like this:

 #if defined(COMPILER_GCC) #define Weak_b #define Weak_e __attribute__((weak)) #elif defined(COMPILER_FOO) #define Weak_b __Is_Weak #define Weak_e #endif #define DEFINE_DELETE_OBJECT(type) \ Weak_b void delete_ ## type_(int handle) Weak_e; \ Weak_b void delete_ ## type(int handle) Weak_e; 

In case it is not obvious that you want to define Weak_b and Weak_e as start and end bracketing constructors, because some compilers, such as GCC, add attributes as a complement to the type signature, and some, for example, MSC, add as a prefix ( or at least once, these have been the years since I used MSC). The bracketing contexts allow you to define what always works, even if you need to pass the entire type signature to the compiler construct.

Of course, if you try to port this to the compiler without the attributes you need, you won’t be able to do anything, but leave the macros nowhere and hope that your code will still work. In the case of a pure warning or optimization of pragmas, this is most likely. In other cases, not so much.

Oh, and I suspect that you really need to define Weak_b and Weak_e as macros that take parameters, but I didn’t want to read the documents to create a weak definition just for this example. I leave this as an exercise for the reader.

0
Jun 12 2018-10-12T00:
source share

Is there a way to insert a pragma statement into a macro with other statements?

No, you cannot put preprocessor instructions in preprocessor instructions. However, you could include it in the inline function. However, this strikes the C tag.

-3
Jun 12 2018-10-12
source share



All Articles