c-preprocessor

How to expand macro and delete comma

99封情书 提交于 2020-01-23 02:04:05
问题 For example I want to write my own printf() alternative, but I have to perform calculations on the variable arguments: #define log(fmt_string, ...) my_log(fmt_string, pack_args(__VA_ARGS__), __VA_ARGS__) where pack_args(...) - is a macro too. How should I change this code to handle the only fmt_string presence scenario? log("Some message here"); 回答1: In P99 I have two macros #define P00_ARG( \ _1, _2, _3, _4, _5, _6, _7, _8, \ _9, _10, _11, _12, _13, _14, _15, _16, \ ... etc ... \ _153, _154,

C Preprocessor Remove Trailing Comma

做~自己de王妃 提交于 2020-01-22 17:24:07
问题 I have a macro like this: #define C( a... ) ( char *[] ){ a, 0 } This works for non-empty arguments: C( "a", "b" ) => ( char *[] )( "a", "b", 0 } But I want to remove the trailing comma when provided with an empty argument: C() => ( char *[] ){ , 0 } Is this possible? 回答1: At least in GCC 5.4.0, on Cygwin (default -std=gnu11), this appears to do what you want (assuming I understand your question correctly): #define C( a... ) ( char *[] ){ a 0 } ^ no comma! C( "a", "b", ) ^ comma here => (

How to stringify a string which contains a comma?

╄→尐↘猪︶ㄣ 提交于 2020-01-22 16:00:29
问题 I want to pass a version string in the compile command: $ g++ -Wall -D VERSION="2013-12-03 02:15:21, commit cb060df" -o main main.cpp Inside my code, I have the following: #define TOSTR_(x) #x #define STRINGIFY(x) TOSTR_(x) #define VERSION_STR STRINGIFY(VERSION) This doesn't work, because the VERSION macro has a comma in it, so it looks like I'm passing two arguments to TOSTR() (apparently, the VERSION macro only gets expanded after it's passed to STRINGIFY() as one unique argument). The

Constant in enum not seen in define [closed]

南笙酒味 提交于 2020-01-22 03:43:13
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I have such a code in C enum { MYVAR = 1 }; #ifdef MYVAR #define VAR 1 #else #define VAR 2 #endif printf("VAR = %d", VAR); in this case it will prints "VAR = 2". Is there any way to get preprocessor see the definition in enum ? 回答1: No, this is not possible. #ifdef and #if are part of preprocessor, which

Evaluate all macros in a C++ header file

孤者浪人 提交于 2020-01-22 00:14:38
问题 I have a requirement to build an automated system to parse a C++ .h file with a lot of #define statements in it and do something with the value that each #define works out to. The .h file has a lot of other junk in it besides the #define statements. The objective is to create a key-value list, where the keys are all the keywords defined by the #define statements and the values are the evaluations of the macros which correspond to the definitions. The #defines define the keywords with a series

Evaluate all macros in a C++ header file

狂风中的少年 提交于 2020-01-22 00:14:30
问题 I have a requirement to build an automated system to parse a C++ .h file with a lot of #define statements in it and do something with the value that each #define works out to. The .h file has a lot of other junk in it besides the #define statements. The objective is to create a key-value list, where the keys are all the keywords defined by the #define statements and the values are the evaluations of the macros which correspond to the definitions. The #defines define the keywords with a series

GCC preprocessor output and compilation in one pass

爷,独闯天下 提交于 2020-01-21 04:14:49
问题 Is it possible to generate preprocessor output and compilation in one step with GCC? Something like: gcc -E -c main.cc -o main.o that would generate main.o and main.i 回答1: Yes. Look at gcc -save-temps option. It compiles the source file and saves the result of the preprocessing in a .i file. (It also saves the result of the assembler phase to a .s file). gcc -save-temps -c main.cc -o main.o will generate main.o but also main.i and main.s . main.i is the result of the preprocessing. 回答2: No,

Include-guards and commented out Doxygen comments affect the output of Doxygen

我只是一个虾纸丫 提交于 2020-01-17 05:55:51
问题 The behavior of Doxygen 1.8.11 for the following example is really bizarre: File test.h : /// \file /// \namespace N1 /// Namespace N1. namespace N1 { #include "a.h" } // namespace int main() { return 0; } File a.h : /// \file /// \namespace N1::N2 /// Namespace N2 namespace N2 { #include "b.h" } // namespace File b.h : /// \file // class A does not appear without the commented Doxygen comments below! // /// \cond None #ifndef GUARD #define GUARD // /// \endcond /// Class A class A {}; // ///

C++ preprocessor--join arguments

六眼飞鱼酱① 提交于 2020-01-17 05:38:19
问题 Is there a way to make the C++ preprocessor join arguments with a joiner token? I've learned that I can do: #include <boost/preprocessor/seq/cat.hpp> #define arg1 foo #define arg2 bar #define arg3 baz BOOST_PP_SEQ_CAT((arg1)(_)(arg2)(_)(arg3)) to get foo_bar_baz . I have two questions: Is there a way to do it for without the repeated explicit joiner characters ( (_) ) and for an argument list of variadic length? Is it necessary to pass the arguments like so: (arg1)(arg2)(arg3) Can I wrap it

Xcode version preprocessor

為{幸葍}努か 提交于 2020-01-15 02:40:37
问题 I have a small bug in my application that only exists when building with Xcode 6. I fixed this bug, but then this part is bugged when building with Xcode 5. So there is an ugly battle between the two Xcode versions.. Now.. I want to check which Xcode version is being used to build the project via a preprocessor. Is this possible to do so? If so, this would fix my problem really fast. 来源: https://stackoverflow.com/questions/28765740/xcode-version-preprocessor