c-preprocessor

Cmake - Want to see intermediate .i files

匆匆过客 提交于 2020-01-13 13:50:50
问题 I want to know how to make Cmake give me a target that will allow me to save the .i files from my C program with the macro expansion, etc completed. Will I need to make a custom target to do this? 回答1: If your are using the Makefile generator, then there are already targets for .i files. Type make help , and you will see all the targets, including those suffixed by .i , .s , and .o . 回答2: If you don't want to manually run the make targets and filter for .i extensions, you could do it like

C-preprocessor: iteratively expand macro to comma-separated list

别说谁变了你拦得住时间么 提交于 2020-01-13 10:44:11
问题 Using Paul Fultz II's solution in the post C-preprocessor recursive macro, I'd like to expand an unlimited number of parenthesized macro arguments, e.g. #define MY_CHAIN (alpha) (beta) (gamma) into a comma-separated list which can be passed to a variadic macro, e.g. CHAIN_COMMA(MY_CHAIN) // alpha, beta, gamma I'm able to expand into braces [alpha] [beta] [gamma] and delimit the list with everything I've tried except a comma, alpha :: beta :: gamma in the example below. Here is my full

C++ Preprocessor string literal concatenation

孤者浪人 提交于 2020-01-13 09:33:19
问题 I found this regarding how the C preprocessor should handle string literal concatenation (phase 6). However, I can not find anything regarding how this is handled in C++ (does C++ use the C preprocessor?). The reason I ask is that I have the following: const char * Foo::encoding = "\0" "1234567890\0abcdefg"; where encoding is a static member of class Foo . Without the availability of concatenation I wouldnt be able to write that sequence of characters like that. const char * Foo::encoding = "

Macro to compute number of bits needed to store a number n

风格不统一 提交于 2020-01-13 08:13:10
问题 Let's say I need to write C macro that returns number of bits(1..32) needed to store unsigned 32-bit integer. (Result equals ceiling(log2(n)). I need it as compile-time computed macro, not a function. I could do #define NBITS(n) ((n)&(1<<31)?32:(n)&(1<<30)?31:... it works, but is rather long. (Speed does not matter here, computation is at compile time). Is there shorter way to write this macro ? Shortest ? 回答1: #define NBITS2(n) ((n&2)?1:0) #define NBITS4(n) ((n&(0xC))?(2+NBITS2(n>>2)):

Macro to compute number of bits needed to store a number n

大城市里の小女人 提交于 2020-01-13 08:13:08
问题 Let's say I need to write C macro that returns number of bits(1..32) needed to store unsigned 32-bit integer. (Result equals ceiling(log2(n)). I need it as compile-time computed macro, not a function. I could do #define NBITS(n) ((n)&(1<<31)?32:(n)&(1<<30)?31:... it works, but is rather long. (Speed does not matter here, computation is at compile time). Is there shorter way to write this macro ? Shortest ? 回答1: #define NBITS2(n) ((n&2)?1:0) #define NBITS4(n) ((n&(0xC))?(2+NBITS2(n>>2)):

Macro-function to generate macro with prefix (avoid stringification)

别来无恙 提交于 2020-01-13 07:21:35
问题 Background I have a project where I have two separate products with near-identical macro names, for which I would like to create a macro-like-function to retrieve the values of the macros quickly. I have written a getTranslation macro-function to take the literal text provided to the "function", which should be treated as a string and a string prefix (shown below). Question How can I accomplish this operation of taking the arguments supplied to the macro, concatenating them together (with an

Can __FILE__ and __LINE__ be made linkable when printed to Qt Creator's debug console?

北战南征 提交于 2020-01-12 14:41:48
问题 Header: #define TRACE_ERROR(s) \ { ... char TraceBuffer[512]; sprintf(TraceBuffer, "%s\t(%s:%d)", s, __FILE__, __LINE__); DebugErrTrace(TraceBuffer); ... } Implementation: void DebugErrTrace(char *String, ...) { ... qDebug() << String; } The above spits out a line of debug trace, which might look something like ERROR File Missing! (..\trunk\Common\FileManager.cpp:102) in Qt Creator's debug console. I've noticed that Qt's own error messages e.g. Object::connect: No such slot cClass::Method

Can __FILE__ and __LINE__ be made linkable when printed to Qt Creator's debug console?

◇◆丶佛笑我妖孽 提交于 2020-01-12 14:41:00
问题 Header: #define TRACE_ERROR(s) \ { ... char TraceBuffer[512]; sprintf(TraceBuffer, "%s\t(%s:%d)", s, __FILE__, __LINE__); DebugErrTrace(TraceBuffer); ... } Implementation: void DebugErrTrace(char *String, ...) { ... qDebug() << String; } The above spits out a line of debug trace, which might look something like ERROR File Missing! (..\trunk\Common\FileManager.cpp:102) in Qt Creator's debug console. I've noticed that Qt's own error messages e.g. Object::connect: No such slot cClass::Method

Why is this nested macro replacement failing?

二次信任 提交于 2020-01-12 14:33:11
问题 I am trying to apply the X Macro concept, in order to have the possibility to initialize all struct members to a custom default (invalid) value. I write the following code: #define LIST_OF_STRUCT_MEMBERS_foo \ X(a) \ X(b) \ X(c) #define X(name) int name; struct foo { LIST_OF_STRUCT_MEMBERS_foo }; #undef X #define X(name) -1, static inline void foo_invalidate(struct foo* in) { *in = (struct foo){ LIST_OF_STRUCT_MEMBERS_foo }; } #undef X #define X(name) -1, #define foo_DEFAULT_VALUE { LIST_OF

Why is this nested macro replacement failing?

徘徊边缘 提交于 2020-01-12 14:32:04
问题 I am trying to apply the X Macro concept, in order to have the possibility to initialize all struct members to a custom default (invalid) value. I write the following code: #define LIST_OF_STRUCT_MEMBERS_foo \ X(a) \ X(b) \ X(c) #define X(name) int name; struct foo { LIST_OF_STRUCT_MEMBERS_foo }; #undef X #define X(name) -1, static inline void foo_invalidate(struct foo* in) { *in = (struct foo){ LIST_OF_STRUCT_MEMBERS_foo }; } #undef X #define X(name) -1, #define foo_DEFAULT_VALUE { LIST_OF