c-preprocessor

Detect presence or absence of arguments in a C macro

浪子不回头ぞ 提交于 2019-12-06 03:29:36
问题 How can one define a C macro IFARGS(YES, NO, ...) such that invoking IFARGS with no additional arguments produces NO , and invoking IFARGS with one or more arguments produces YES ? I have an answer using GCC (see below), but I'd prefer one for C99 if possible (or a proof of its impossibility). 回答1: In C99 it is possible to detect if a macro argument is empty, but making that robust against all odds that may appear in that argument (arguments that are themselves expanding, contain () and stuff

Cmake - Want to see intermediate .i files

一世执手 提交于 2019-12-06 02:55:38
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? 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 . If you don't want to manually run the make targets and filter for .i extensions, you could do it like this: for subdir in */ do (cd $subdir && make $(make help |& grep \\.i$ | cut -c4-)) done Alternatively, I sometimes

Partially processing a file with the preprocessor [duplicate]

你。 提交于 2019-12-06 02:44:20
问题 This question already has answers here : Is there a C pre-processor which eliminates #ifdef blocks based on values defined/undefined? (5 answers) Closed 2 years ago . We have inherited a very convolved project (500kloc) with a lot of preprocessor conditional logic, most of which is no longer relevant, and I want to clean it up. Can I use the preprocessor¹ to expand only some of the conditional logic, and leave all other preprocessor macros, defines, and includes alone in the output? ¹ Here,

gcc assembler preprocessor not compatible with standard headers

谁都会走 提交于 2019-12-06 02:11:47
问题 The man page for gcc states file.s Assembler code. file.S file.sx Assembler code that must be preprocessed. And many standard include files have #ifndef __ASSEMBLY__ ... #endif wrappers to allow inclusion from assembly files. I could have sworn I've written programs before with gcc and it defined this when assembling, but now I'm running into problems. Here's some test code: test.S #include <sys/syscall.h> #include <asm/signal.h> .intel_syntax noprefix .text .global foo // int foo(int pid)

Change member-typedef depending on template parameter?

偶尔善良 提交于 2019-12-06 02:08:07
问题 I have this problem here that I can’t figure out how to solve. I want a template class that takes an integer as template parameter and sets the template parameters for another class accordingly: template <int T> class Solver { public: #if T <= 24 typedef MyMatrix<float> Matrix; #else if T <= 53 typedef MyMatrix<double> Matrix; #else typedef MyMatrix<mpreal> Matrix; #endif Matrix create(); }; And then calling it like this: Solver<53>::Matrix m = Solver<53>::create(); How can I do something

Strip Linux kernel sources according to .config

那年仲夏 提交于 2019-12-06 01:48:26
问题 Is there any efficient way (maybe by abusing the gcc preprocessor?) to get a set of stripped kernel sources where all code not needed according to .config is left out? 回答1: Well got some steps into a solution. First, one can obtain the used compiler commands by make KBUILD_VERBOSE=1 | tee build.log grep '^ gcc' build.log For now, I select only one gcc command line for further steps. For example the build of kernel/kmod.c, it looks like: gcc <LIST OF MANY OPTIONS> -c -o kernel/kmod.o kernel

Is there a way to apply an action to N C++ class members in a loop over member names (probably via pre-processor)?

青春壹個敷衍的年華 提交于 2019-12-05 23:57:51
问题 The problem: I have a C++ class with gajillion (>100) members that behave nearly identically: same type in a function, each member has the same exact code done to it as other members, e.g. assignment from a map in a constructor where map key is same as member key This identicality of behavior is repeated across many-many functions (>20), of course the behavior in each function is different so there's no way to factor things out. The list of members is very fluid, with constant additions and

Function-like macro definition in C

泪湿孤枕 提交于 2019-12-05 23:46:00
问题 I'd like to define a function like MACRO . i.e. #define foo(x)\ #if x>32\ x\ #else\ (2*x)\ #endif that is, if x>32, then foo(x) present x else, foo(x) present (2*x) but my GCC complains about: int a = foo(31); I think C preprocessor should be handle this correctly. since at compile time, it knows x=33 . it could replace foo(33) with (2*33) 回答1: You can as follows #define foo(x) ((x) > 32 ? (x) : (2 * (x))) But that evaluates x multiple times. You can instead create a static function, which is

Why does stringizing an euro sign within a string literal using UTF8 not produce an UCN?

╄→尐↘猪︶ㄣ 提交于 2019-12-05 23:27:16
问题 The spec says that at phase 1 of compilation Any source file character not in the basic source character set (2.3) is replaced by the universal-character-name that designates that character. And at phase 4 it says Preprocessing directives are executed, macro invocations are expanded At phase 5, we have Each source character set member in a character literal or a string literal, as well as each escape sequence and universal-character-name in a character literal or a non-raw string literal, is

C++ Compare template type during compile time

半腔热情 提交于 2019-12-05 21:55:02
I have a template class. Since the templates are processed during compile time, is it possible to compare the template parameter during compile time and use the preprocessor to add specific code? Something like this: template<class T> class MyClass { public: void do() { #if T is equal to vector<int> // add vector<int> specific code #if T is equal to list<double> // add list<double> specific code #else cout << "Unsupported data type" << endl; #endif } }; How can I compare the template types to another type during compile time as shown in the example above? I do not want to add specific