5,965 questions
0
votes
1
answer
108
views
Removing the commas in a variadic macro
I want to remove the comma characters inside __VA_ARGS__.
I have found a FOR_EACH implementation, which kind of solves it, but gives cluttered error messages when there is a mistake.
My problem has ...
Advice
0
votes
6
replies
199
views
C++ compile type endianness for use in macros
I need to layout a structure in two different ways depending on the endianness of the target platform. Currently I'm using an additional pre-compile phase to run a program to test the endianness, and ...
-4
votes
1
answer
82
views
C++ preprocessor VARARGS - invalid syntax in a NodejS header file
I'm experimenting a bit with trying to build NodeJS 24.11.1 with GCC 13.4.0 on an older OS X system, mostly as a can-it-be-done project.
Regardless of the reasons, among the hurdles that could be ...
5
votes
1
answer
167
views
Why does the same code, one generated by macros and the other handwritten, produces different results in MSVC?
The same code, one generated by macros and the other handwritten, produces different results.I'm lost and don't know how to fix it.
my environment is follow:
vs2019, msvc2019_64, c++14
if using ...
2
votes
1
answer
115
views
C macro compilation fails in Clang with "expected ;" due to phantom comma in clang -E expansion
I wrote my "Result" macro header, using C23 and some of the newest features:
#pragma once
#include <stdio.h>
#include <stdlib.h>
#define __RESULT_EAT_PARENS(...) __VA_ARGS__
...
1
vote
1
answer
64
views
C preprocessor and the DEFER technique
This snippet is mostly from the Cloak wiki, C Preprocessor tricks, tips and idioms, with DEFER2, X1, and X2 added by me for ease of referencing things in this post.
#define EMPTY()
#define DEFER(id) ...
8
votes
6
answers
872
views
Template for function with N inputs
If it's possible, I would like to define a macro that would take number of arguments in function template ARGS_COUNT and make a template with all argument types being enumerated and output being ...
0
votes
2
answers
111
views
variadic macro that references another variadic macro is not expanding the way I want [closed]
I have
#file debug.h
#ifndef PDEBUG
#define PDEBUG(msg ...)\
logger_write(logger_stdout, (struct log) {\
.level_target = LOGLEV_DEBUG,\
.message = msg,\
.properties = strdict_make_from_va(...
3
votes
2
answers
150
views
GCC pragma: error: expected expression before ‘#pragma’
Please consider the following minimal example:
#include <stdio.h>
// Compile with:
// gcc -g -o myprogram main.c
//#define SPECIAL
typedef struct {
int id;
char name[50];
float value;
} ...
-2
votes
1
answer
139
views
Is this a C preprocessor bug? [duplicate]
#include <stdio.h>
#define n 16
#define a -2.0
#define b 2.0
#define c (b-a)/n
int main() {
printf("%lf\n",5.0*c);
}
Outputs 1.250000
But if i change (b-a)/n to b/n-a/n it outputs ...
5
votes
1
answer
127
views
How to create meaningful error messages from _Generic macros?
Lets say I have some manner of type safety macro based on _Generic, which I use to wrap an actual function call to make it type safe:
#include <stdio.h>
void print (int i)
{
printf("%d\n&...
1
vote
1
answer
172
views
#error "Please port gnulib freadahead.c to your platform!" while compiling openwrt
Im trying to build old version of openwrt and Im getting this error #error "Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread, ungetc on your system, then ...
4
votes
1
answer
181
views
In which situations does the second form of `__has_include` appear?
Problem Description
C++17 introduced the __has_include preprocessor expression, which has the following two forms:
__has_include(header-name)
__has_include(header-name-tokens)
For the first form, ...
0
votes
1
answer
108
views
Conditional compilation depending on the size of data type [duplicate]
This does not answer my need. I do not want to generate an error, I want to adapt the behavior of the software depending on the condition.
Example:
#if ( sizeof(unsigned long) == 4 )
# define a 5
#...
5
votes
2
answers
126
views
How does C90 treat non-directives / unrecognised preprocessing directives?
I'm confused about the behaviour of unrecognised preprocessor directives according to specifically the C90 standard.
In C90, a group-part is defined as either (with question marks used to indicate its ...
4
votes
2
answers
139
views
C macro expansion order (need to understand why) [closed]
Consider the macros:
#define B(y,z) B##y##z
#define C(z) C##z
#define A(x) B(x, C(x))
A(1)
Everywhere I look, people say arguments are replaced first. Which implies this should happen:
A(1) => B(1,...
4
votes
2
answers
228
views
How to accurately count tokens in a C program? Do preprocessor directives like #include and #define count?
I'm studying how tokenization works in C programming, especially for examination purposes. I want to understand how many tokens are present in the following C code and how preprocessor directives are ...
1
vote
3
answers
155
views
LOG macro that logs named variables
In my code I use printf-like LOG(const char* fmt, ...) macro. Under the hood it prints/logs to different output destinations (file, console, debugger output).
It's my own macro, so it does printf ...
0
votes
1
answer
25
views
In C, how do I use a single hex value in both an enum and a const char *
I have a function that allows access to system variables based on a lookup code. The lookup code has an enum type.
typedef enum SystemVariable_e {
INT_SYSTEM_VAR_1 = 0x10,
INT_SYSTEM_VAR_2 = 0x20
...
2
votes
1
answer
147
views
Macro expansion order in C
#define STRIP_PARENS(...) __VA_ARGS__
#define L(X) \
X((a, b)) \
X((c, d))
#define FIRST(x, ...) x
#define FA_INNER(...) FIRST(__VA_ARGS__)
#define FA(x, ...) FA_INNER(x)
#define FAL(args) ...
6
votes
0
answers
128
views
How to avoid macros from X11 polluting other third party code?
Is there a nice way to wrap X11/Xlib.h and X11/X.h includes so they don't leak the following macros everywhere? It's an endless source of pain:
#define Bool int
...
#define None 0L /* ...
2
votes
3
answers
164
views
What characters are the legal terminators of C preprocessor keywords like #if #else #elif #endif
What characters are the legal terminators of C preprocessor keywords/tokens like #if, #ifdef, #ifndef, #else, #elif, #endif, etc...
#ifdef COMING
printf("Hello World\n");
#else
printf(&...
3
votes
0
answers
163
views
How to make a macro in C to parameterize register?
I want to define a assembly to read register from arm64. Code is like the following:
asm volatile(
"str <reg> [%0]\n"
:
: "r"(value)
: "...
4
votes
3
answers
204
views
"Extensible" C generics
I'm trying to make an implementation of a Vector in C (yes, I know, this has been done to death), generic across any types used. So far I have an implementation something like:
#define Vector(T) ...
13
votes
5
answers
3k
views
In C++, can I use a preprocessor directive inside of a statement on one line, like I can do in Delphi?
The C/C++ ternary operator is great:
int x = (someFlag) ? -1 : 1;
Delphi has a nice feature where preprocessor directives can be written on one line. Two examples:
x := {$IFDEF DBG_MY_SYMBOL} -1 {$...
3
votes
2
answers
133
views
Accessing structure member that is identical to macro name
Is there a way to access a structure member which is identical to a macro name? Consider this code:
struct test {
int foo;
};
#define foo bar
int main(int argc, char *argv[])
{
struct test ...
0
votes
0
answers
38
views
C++ Processing each argument of variadic macro individually [duplicate]
I have a two macros - MACRO1(x, y) and MACRO2(x, ...)
I want for MACRO2(x, a, b, c...) to expand into MACRO1(x, a), MACRO1(x, b), MACRO1(x, c), ... but can't wrap my head around how to do that. I've ...
4
votes
1
answer
122
views
Google Test: How to get correct line numbers in test output for parameterized tests?
I'm using Google Test with parameterized tests and want to see the correct line number in the test output so I can quickly find the specific test case that failed.
Problem
When a test fails, the ...
1
vote
0
answers
122
views
nvim treesitter highlight for "#if...#elif...#endif" preprocessors
I am using nvim + nvim-treesitter to parse and highlight my code. There is a problem on C source code #if...#elif...#endif preprocessor block.
Say I have the following code
// test.c
#include "...
3
votes
2
answers
125
views
Can a C program execute successfully if main() is defined as a macro?
We know that main() is the entry point of a C program. But what happens if we define main as a macro, like this?
#define main something_else
Will the program compile and run correctly?
How do ...
6
votes
1
answer
98
views
Why does an empty preprocessor command still evaluate to something?
Please take a look at this small code snippet:
#include <stdio.h>
#define BAR
int main(int argc, char *argv[])
{
int foo = 1;
foo = BAR(1, 2, 3);
printf("%d\n", ...
-1
votes
1
answer
83
views
Using the Boost preprocessor to statically initialize an array
How can I use the Boost preprocessor to statically initialize an array with string and pointer pairs as shown below
#include <string>
struct Node;
struct Port
{
Port(std::string name_, ...
3
votes
1
answer
124
views
Double Quotes not escaped in eclipse shell command for preprocessor define
I'm using eclipse on Windows to build a C-project.
I create a preprocessor define containing git status summary git status -s.
This is my define symbol:
GIT_STATUS="$(shell git status -s)"
...
1
vote
1
answer
109
views
Lambda with structured bindings inside macro call
This may be related to How to correctly forward structured binding arguments in a macro , but I feel the real problem is not the same, so may have a different solution.
I want to pass a lambda as a ...
1
vote
2
answers
174
views
How does the C preprocessor handle not repeating includes and copying more than once
I recently learned how the c compiler actually works and there is a specific configuration that i do not understand : When I write a header it looks like this:
#ifndef HEADERNAME
#define HEADERNAME
//...
2
votes
1
answer
98
views
Make a preprocessor string macro NOT expand things within the string
prog.c has
#include <stdio.h>
#define STR(x) _STR(x)
#define _STR(x) #x
int main()
{
const char *s = STR(MYPATH);
puts(s);
}
and is compiled with cc -...
0
votes
0
answers
168
views
Portable macro that declares parametrized user define literal
I want to define a preprocessor macro with parameter N that constructs a declaration for C++ user-defined literal with suffix _uN.
#define DEFINE_LITERAL(N) int operator""_u##N(const char* s)...
1
vote
1
answer
113
views
How to prevent C++ macro from substituting a class member?
I am building a Linux kernel module, the source code is C++ based.
One of the src files named A.cpp always has a build failure.
A.cpp indirectly includes two header files: asm/current.h and bits/...
2
votes
1
answer
114
views
Can you compare two constant pointers in the C preprocessor?
Say my code looks like this:
gpios.h:
#define GPIO_BASE 0x08000100
#define GPIOA ((GpioRegs *) (GPIO_BASE + 0x04))
#define GPIOB ((GpioRegs *) (GPIO_BASE + 0x0C))
#define GPIOC ((GpioRegs *) (...
0
votes
0
answers
63
views
Using the C preprocessor, how to warn only once on macro usage?
I would like to display a warning message when the user calls a macro, but only once per compilation unit, so they don't drown in error messages.
Something like that:
int main() {
MACRO(42); // <-...
1
vote
2
answers
100
views
Is the C preprocessor part of the C standard?
I just know that #pragma once is not part of the C standard but rather part of the preprocessor.
In that case, does that make the preprocessor not part of the C standard?
So, are the C programming ...
1
vote
1
answer
90
views
Getting the type of every argument in a variadic macro argument list?
I have a macro like this:
#define ptapply(pt, ret_type, identifier, ...) ((ret_type (*)()) pget(pt, identifier))(pt __VA_OPT__(,) __VA_ARGS__)
pget returns a void * which I cast into a function ...
1
vote
3
answers
168
views
Ignore the use of a C++ function during compilation having operator<<
I'm developing an API looking like GLib logging.
My API can also disable these functions when the flag -DDEBUG isn't passed to GCC.
That means, there are no residues in the binary, all these debug ...
0
votes
2
answers
128
views
Is it possible to trigger a #if macro if two or more definitions are defined?
For instance, imagine you want to enable only one compilation target so you do something like this:
// uncomment ONE of these #defines to compile for a specific target
#define TARGET_1
#define ...
0
votes
3
answers
80
views
C preprocessor name definition
I want to create some kind of smart name definition using a preprocessor in my library project. So I have for example a timer named TC0 and some additional names for interrupts handler like ...
2
votes
1
answer
2k
views
How do I define a C/C++ preprocessor variable in Meson?
I want to conditionally define a variable that I then can use within C/C++ sources to conditionally compile some code, like this:
#ifdef MY_MESON_VARIABLE
// some code
#endif
How do I go about ...
4
votes
1
answer
153
views
Is there a way to conditionally include a header or import a module depending on a switch/flag?
I've tried:
#ifdef USE_CPP20_MODULES
#define IMPORT_OR_INCLUDE(module_name, include_filepath) \
import module_name;
#else
#define IMPORT_OR_INCLUDE(module_name, include_filepath) \
#include ...
4
votes
2
answers
187
views
Detect a parenthesized macro argument in C
I want a macro to detect whether the passed arg is surrounded by parentheses or not, and call a different macro if so.
e.g.
#define FOO(obj) BAR obj
#define BAR(...) func(__VA_ARGS__)
#define BAR
If ...
0
votes
1
answer
113
views
run only preprocessor using ESP-IDF
I would like to see my esp-idf code after preprocessing (see which code inside of #ifdef will be compiled).
I tried adding "add_compile_optins(-E)" in my main CMake file, but the build ...
1
vote
0
answers
124
views
How to make immutability a default behaviour with C preprocessor
I think that the benefits of data immutability outweigh the inconvenience of the implicit mut qualifier. That's why in my opinion it should be the default. I know that some functional languages ...