c-preprocessor

Concatenate multiple tokens for X macro

北慕城南 提交于 2019-12-11 06:18:11
问题 I'm trying to use X macros and preprocessor concatenation, both for the first time, together. I've read a lot of the other questions on SO related to preprocessor concatenation but not yet been able to wrap my head around them or how to adapt those to my use case. The list of items is a list of ID numbers for a bunch of structs , like so: #define LIST_OF_ID_NUMS \ X(1) \ X(2) \ X(3) \ X(4) \ X(5) \ X(6) \ X(7) \ X(8) \ X(9) \ X(10) \ X(11) I can declare the structs like so: #define X(id_num)

How to show 'preprocessed' code ignoring includes with GCC

一笑奈何 提交于 2019-12-11 05:57:42
问题 I'd like to know if it's possible to output 'preprocessed' code wit gcc but 'ignoring' (not expanding) includes: ES I got this main: #include <stdio.h> #define prn(s) printf("this is a macro for printing a string: %s\n", s); int int(){ char str[5] = "test"; prn(str); return 0; } I run gcc -E main -o out.c I got: /* all stdio stuff */ int int(){ char str[5] = "test"; printf("this is a macro for printing a string: %s\n", str); return 0; } I'd like to output only: #include <stdio.h> int int(){

How to pass a macro's result to another macro?

独自空忆成欢 提交于 2019-12-11 05:05:30
问题 I have two macros in my C code the helps me to compose the name of certain variables. As an example, consider the following: #define MACROA(name) A_##name #define MACROB(name) B_##name void *MACROB(MACROA(object)); So, I'm trying to declare a variable called B_A_object . However, this doesn't work and the compiler throws me the message: object.c:27:21: error: a parameter list without types is only allowed in a function definition void *MACROB(MACROA(object)); ^ object.c:26:26: note: expanded

Use the preprocessor to convert code into a string

风流意气都作罢 提交于 2019-12-11 04:55:20
问题 Disclaimer: I am not a C programmer. I have recently seen a friend's project. Due to reasons I don't understand, he writes code in a string which is compiled at runtime. This results in something like: char x[] = "int y = 5; printf(\"%i\", y)"; run_this_code(x); Which is horrible to use because Visual Studio doesn't step in and do syntax highlighting etc. Using some preprocessor abuse, it is possible to do trick Visual Studio into thinking you're writing real code and then having the

C preprocessor insert/append token in string literal

╄→гoц情女王★ 提交于 2019-12-11 04:51:53
问题 I have the following macro: #define ASSERT_ITERATOR_VALUE_TYPE(Iterator__, Value_type__) \ static_assert(std::is_same<Value_type__, typename Iterator__::value_type>::value, \ "Expected iterator with value type #Value_type__") In the macro above I'm trying to insert/append the Value_type__ token in the string literal that's feed in as the second input argument in static_assert . Obviously, this is not what I'm trying to achieve, since if I state the macro as: ASSERT_ITERATOR_VALUE_TYPE(std:

Define an assert which is effective even if NDEBUG is defined

六眼飞鱼酱① 提交于 2019-12-11 04:44:05
问题 I would like to define an assert macro which is identical to the standard assert(3) call except that it isn't removed by the pre-processor when NDEBUG is defined. Such a call, let us call it assert2 here, is useful for example if you want to have some checks happen in release versions of software as well. How can I do this in a reasonably portable way? I could always just completely re-create the assert mechanism, like 1 : #define assert2(cond) cond ? (void)0 : die_now(#cond, __FILE__, __LINE

Using an openmp pragma inside #define [duplicate]

隐身守侯 提交于 2019-12-11 04:31:59
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: C/C++ pragma in define macro Conditional “pragma omp” How can I use an OpenMP pragmas inside a macro definition? E.g. #define A() { \ ...a lot of code... \ #pragma omp for \ for(..) \ ..do_for.. \ ...another a lot of code \ } 回答1: As it was answered here Conditional "pragma omp" C99 has the _Pragma keyword that allows you to place what otherwise would be #pragma inside macros. Something like #define OMP_PARA

Can #if pre-processor directives be nested in C++?

孤者浪人 提交于 2019-12-11 04:28:10
问题 I have a question about Pre-processor directives in c++: For example: #ifndef QUESTION //some code here #ifndef QUESTION //some code here #endif #endif Can we use it in this way, and can the C++ compiler match the ifndef and endif in the right way? 回答1: Yes, we can. The #endif statement matches to the previous #if #ifdef or #ifndef etc for which there hasn't been a corresponding #endif . e.g. #if ----------| #if -----| | #endif ---| | #endif --------| 回答2: Yes, you can nest #if / #endif

How do I enable the preprocessor in gcc assembly

二次信任 提交于 2019-12-11 03:25:53
问题 I am using the GCC like this: gcc -std=gnu99 -fno-leading-underscore -m32 -c -o obj/entry.o src/entry.s However, when I compile the linker says: ld -melf_i386 -T kernel.ld -o kernel obj/entry.o obj/init.o obj/entry.o:(multiboot+0x0): undefined reference to `MB_MAGIC' obj/entry.o:(multiboot+0x4): undefined reference to `MB_FLAGS' obj/entry.o:(multiboot+0x8): undefined reference to `MB_CHECKSUM' Those references are defined in the entry.s file with the preprocessor: #define MB_MAGIC 0x1badb002

How to force macro to not expand

我与影子孤独终老i 提交于 2019-12-11 03:09:17
问题 Using the following code: #include <stdio.h> typedef struct { int APB1ENR; int b; int c; } RCC_TypeDef; typedef struct { int a; int b; int c; } USART_TypeDef; #define USART2_BASE 0x1000 #define USART2 ((USART_TypeDef *) USART2_BASE) #define RCC_BASE 0x2000 #define RCC_APB1ENR_USART2EN_Pos (17U) #define RCC_APB1ENR_USART2EN_Msk (0x1UL << RCC_APB1ENR_USART2EN_Pos) #define RCC_APB1ENR_USART2EN RCC_APB1ENR_USART2EN_Msk #define RCC ((RCC_Typedef *) RCC_BASE) #define SET_BIT(REG, BIT) ((REG) |=