c-preprocessor

Overloading a macro

家住魔仙堡 提交于 2020-01-01 17:07:30
问题 I'm trying to overload a macro by the number of parameter. Of course I can't actually overload the macro. I've tried using variadic macros to choose the right macro (using the fact that if __VA_ARGS__ doesn't exist it's supposed to delete the last coma before it - GCC Reference ): #define TEST1() printf("TEST1"); #define TEST2() printf("TEST2"); #define CHOOSER(x, y,FUNC,...) FUNC() #define MANIMACRO(...) CHOOSER(,__VA_ARGS__,TEST1,TEST2) int main(void) { MANIMACRO(1); MANIMACRO(); } The idea

Overloading a macro

我的未来我决定 提交于 2020-01-01 17:07:05
问题 I'm trying to overload a macro by the number of parameter. Of course I can't actually overload the macro. I've tried using variadic macros to choose the right macro (using the fact that if __VA_ARGS__ doesn't exist it's supposed to delete the last coma before it - GCC Reference ): #define TEST1() printf("TEST1"); #define TEST2() printf("TEST2"); #define CHOOSER(x, y,FUNC,...) FUNC() #define MANIMACRO(...) CHOOSER(,__VA_ARGS__,TEST1,TEST2) int main(void) { MANIMACRO(1); MANIMACRO(); } The idea

C/C++ logging facility that supports conditional function calls depending on log-level

六眼飞鱼酱① 提交于 2020-01-01 12:33:07
问题 Some investigation on existing C /C++ logging solutions turned out that Pantheios might be the best in my case, that is lowest overhead if logging is disabled. All the loggers seem to support a kind of a print log message. However, in my case I have a function call that should be avoided if logging is disabled (since it's quite expensive). At the moment I use a very simple logging setup like #ifdef DEBUG_L1 cout << "msg 1" << endl // log level 1 #ifdef DEBUG_L2 printBuffer() // log level 2

Run a “light” preprocessor for GCC

拟墨画扇 提交于 2020-01-01 02:26:33
问题 Is there a way to run the GCC preprocessor, but only for user-defined macros? I have a few one-liners and some #ifdef , etc. conditionals, and I want to see what my code looks like when just those are expanded. As it is, the includes get expanded, my fprintf(stderr) s turn into fprintf(((__getreeent())->_stderr) , etc. 回答1: Call cpp directly, e.g. $ cat >foo.c <<EOF #define FOO #ifdef FOO foo is defined #else foo is not defined #endif EOF $ cpp foo.c # 1 "foo.c" # 1 "<built-in>" # 1 "<command

Preprocessor token expansion [duplicate]

我们两清 提交于 2019-12-31 12:53:08
问题 This question already has answers here : How to concatenate twice with the C preprocessor and expand a macro as in “arg ## _ ## MACRO”? (2 answers) Closed 4 years ago . My mental model of how the preprocessor works is apparently incomplete, and this is driving me crazy. I want to concatenate two tokens, but the second token should be expanded first. #define ANSWER 42 #define FOO foo_ ## ANSWER Here, FOO expands to foo_ANSWER , but I want it to be foo_42 . So I define a MERGE macro in the

Compiling a project (VS 2008) with the /p argument (preprocess to a file) doesn't compile

你说的曾经没有我的故事 提交于 2019-12-31 05:24:22
问题 I have a project in C++ that I would like to view the preprocessor output to see what some #defines and macros would look like. I tried the /p switch to turn on the preprocess to a file option to the compiler (it turns off full compilation and only runs the preprocessor) but my project now refuses to compile and shows a long list of errors starting with: "Cannot open include file: 'stdafx.h': No such file or directory". The project compiles fine without the /p argument, of course. Any

How memory is allocated to macros in c?

寵の児 提交于 2019-12-31 03:28:05
问题 I would like to know how the memory is allocated to #define variables in C. #define VAR1 10 I have 2 questions... What's the type of VAR1? In which memory segment VAR1 is stored? 回答1: VAR1 has neither a type nor any runtime representation. It's only recognized by the preprocessor. So the answer is Mu: your question cannot be answered because it is based on incorrect assumptions. 回答2: In which memory segment VAR1 is stored? In none of the segment. VAR1 is relevant only in pre-processing stage

Run GCC preprocessor non-C files

▼魔方 西西 提交于 2019-12-31 01:59:04
问题 I'm using a proprietary development environment that compiles code written in C , as well as the IEC 61131 languages. For the C compilation, it uses GCC 4.1.2 with these build options: -fPIC -O0 -g -nostartfiles -Wall -trigraphs -fno-asm The compilation is done by a program running on windows utilizing Cygwin. My issue is, IEC language preprocessor is not that useful (doesn't support #define at all) and I want to use macros! I don't see why the GCC preprocessor would really care what language

Working of the C Preprocessor

*爱你&永不变心* 提交于 2019-12-31 00:48:08
问题 How does the following piece of code work, in other words what is the algorithm of the C preprocessor? Does this work on all compilers? #include <stdio.h> #define b a #define a 170 int main() { printf("%i", b); return 0; } 回答1: The preprocessor just replaces b with a wherever it finds it in the program and then replaces a with 170 It is just plain textual replacement. Works on gcc. 回答2: It's at §6.10.3 (Macro Replacement): 6.10.3.4 Rescanning and further replacement 1) After all parameters in

Did `#pragma once` make it into C++0x?

拟墨画扇 提交于 2019-12-30 17:19:12
问题 The title says it all. Have #pragma once been standardized for C++0x? I don't know any compiler that doesn't provide an implementation of it, with almost always the same semantics and name. 回答1: All #pragma directives cause the implementation to behave in an implementation defined way. This hasn't changed between C++03 and the latest C++0x draft (n3225.pdf). Include guards are the portable alternative. 回答2: Sun C++ compiler (Solaris) does not implement it. And no, it's not in C++0x drafts.