c-preprocessor

How to make G++ preprocessor output a newline in a macro?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 11:21:15
问题 Is there a way in gcc/g++ 4.* to write a macro that expands into several lines? The following code: #define A X \ Y Expands into X Y I need a macro expanding into X Y 回答1: Got it! #define anlb /* */ A /* */ B anlb anlb gcc -E -CC nl.c /* */ A /* */ B /* */ A /* */ B 回答2: Make the macro generate a special markup, say __CR__ , then pipe the result of CPP into a script which translates the macro to a true newline, for example, sed 's/__CR__/\n/g' . I just found this useful to generate a code

GCC preprocessor [duplicate]

有些话、适合烂在心里 提交于 2019-12-17 11:16:00
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Running the GCC preprocessor Is there a GCC option to make the GCC preprocessor generate C source code but filter out irrelevant source code? For example, a C file has #define switch to define for many different platforms. I'm only intersted in one platform, so I want the C preprocessor to filter out unrelated code. Does GCC support this? 回答1: Use gcc -E to only run the preprocessor part, e.g. give a file in.c

C preprocessor macro specialisation based on an argument

╄→гoц情女王★ 提交于 2019-12-17 10:34:54
问题 Is it possible to have one macro expanded differently for one specific argument value and differently for all other arguments? Say I define a current user: #define CURRENT_USER john_smith What I want to be able to do is to have a macro that will be expanded differently if user passed matches CURRENT_USER . Mind you that I don't know all possible user a priori. The most basic case: #define IS_CURRENT_USER(user) \ /* this is not valid preprocessor macro */ \ #if user == CURRENT_USER \ 1 \ #else

What does the compiler error “missing binary operator before token” mean?

爱⌒轻易说出口 提交于 2019-12-17 09:46:43
问题 I recently got the following error when trying to compile with gcc: error: missing binary operator before token "(" Web and SO searches came up with several specific examples of this error, with specific code changes to fix them. But I found no general description of what condition causes this error to be issued. When and why does gcc emit this error? 回答1: This is not a compiler error, it is a preprocessor error. It seems to occur when the preprocessor encounters invalid syntax while trying

#ifdef inside #define

爱⌒轻易说出口 提交于 2019-12-17 08:54:33
问题 I am trying to write something like this: #define COV_ON(x) \ #ifdef COVERAGE_TOOL \ _Pragma (COVERAGE #x) #endif Is there any way to define COV_ON like this? I know what I have done above is wrong as I can't have #ifdef inside #define. ( # is not an allowed character in #define ). So is there any solution? 回答1: Not possible. Do it the other way around: #ifdef COVERAGE_TOOL #define COV_ON(x) _Pragma (COVERAGE #x) #else #define COV_ON(x) #endif 回答2: Simply turn it around: #ifdef COVERAGE_TOOL

What is a good reference documenting patterns of use of X-Macros in C (or possibly C++)?

社会主义新天地 提交于 2019-12-17 08:53:08
问题 A basic definition and example and a few references for "X-Macros" is given in this wikipedia entry on the C pre-processor: An X-Macro is a header file (commonly using a ".def" extension instead of the traditional ".h") that contains a list of similar macro calls (which can be referred to as "component macros"). What are some good sources of information on how to use this powerful technique? Are there well-known open source libraries using this method? 回答1: I use X Macros() in code a lot. The

Can the C preprocessor be used to tell if a file exists?

若如初见. 提交于 2019-12-17 08:28:09
问题 I have a very large codebase (read: thousands of modules) that has code shared across numerous projects that all run on different operating systems with different C++ compilers. Needless to say, maintaining the build process can be quite a chore. There are several places in the codebase where it would clean up the code substantially if only there were a way to make the pre-processor ignore certain #includes if the file didn't exist in the current folder. Does anyone know a way to achieve that

C Macros to create strings

ⅰ亾dé卋堺 提交于 2019-12-17 07:22:23
问题 Alternative Titles (to aid search) Convert a preprocessor token to a string How to make a char string from a C macro's value? Original Question I would like to use C #define to build literal strings at compile time. The string are domains that change for debug, release etc. I would like to some some thing like this: #ifdef __TESTING #define IV_DOMAIN domain.org //in house testing #elif __LIVE_TESTING #define IV_DOMAIN test.domain.com //live testing servers #else #define IV_DOMAIN domain.com /

Finding out what the GCC include path is [duplicate]

戏子无情 提交于 2019-12-17 06:24:48
问题 This question already has answers here : What are the GCC default include directories? (4 answers) Closed 5 years ago . I'm trying to programmatically find the #include path on Linux, which as I understand it, in practice means finding what GCC considers it to be. (Is that quite true? How does Clang do it?) According to http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html some of the components involve the CPU architecture and the GCC version; the latter in particular seems tricky; I suppose it

How to use #if inside #define in the C preprocessor?

亡梦爱人 提交于 2019-12-17 06:13:00
问题 I want to write a macro that spits out code based on the boolean value of its parameter. So say DEF_CONST(true) should be expanded into const , and DEF_CONST(false) should be expanded into nothing. Clearly the following doesn't work because we can't use another preprocessor inside #defines: #define DEF_CONST(b_const) \ #if (b_const) \ const \ #endif 回答1: You can simulate conditionals using macro token concatenation as follows: #define DEF_CONST(b_const) DEF_CONST_##b_const #define DEF_CONST