A list of puns related to "C Preprocessor"
It seems to me that the Macro Preprocessor can be so useful for avoiding writing duplicate code, yet to me, optimal and correct usage are not very obvious.
There are lots of tech books written about very small niches, like "Understanding and Using Pointers" (which I personally found a lot less useful than ASM Step by Step).
Yet I haven't found any books about "Mastering C Macros" or something like that.
Of course we can learn everything on our own via trial and error and digging through the infinite codebase that is Github... But I sure find a structured "tutorial"/"exercises" style introduction speeds me up a lot, especially for tools that I haven't used much.
Are there a few chapters of some of the more popular C books that focus on preprocessor macros?
Of course there are websites that I've seen, and if that's the way to go, it would be immensely helpful to have some recommendations for the best ones. The best thing about book versions is that you can read reviews to make sure the author really knows what they're talking about...
Hi! I'm interested in making a D wrapper for GNU Lightning for a part of a hobby language implementation. The problem is I'm immediately hitting a wall in terms of how to translate lightning.h (available in GNU Lightning releases from here to D. It contains myriad preprocessor directives, most of which can be trivially translated to D, but some of which seems somewhat trickier. Take the below code (lines 35-58 in lightning.h):
#ifndef __WORDSIZE
# if defined(WORDSIZE) /* ppc darwin */
# define __WORDSIZE WORDSIZE
# elif defined(__SIZEOF_POINTER__) /* ppc aix */
# define __WORDSIZE (__SIZEOF_POINTER__ << 3)
# elif defined(_ILP32) /* hppa hp-ux */
# define __WORDSIZE 32
# elif defined(_LP64) /* ia64 hp-ux (with cc +DD64) */
# define __WORDSIZE 64
# elif defined(_MIPS_SZPTR) /* mips irix */
# if _MIPS_SZPTR == 32
# define __WORDSIZE 32
# else
# define __WORDSIZE 64
# endif
# else /* From FreeBSD 9.1 stdint.h */
# if defined(UINTPTR_MAX) && defined(UINT64_MAX) && \
(UINTPTR_MAX == UINT64_MAX)
# define __WORDSIZE 64
# else
# define __WORDSIZE 32
# endif
# endif
#endif
I guess the problem might just be that I don't really... get what this code is doing. I know that the builtin D version specifications can probably help me, but would my naive approach of just making a long list of all the builtin versions that would specify 64-bit or 32-bit and assigning __WORDSIZE appropriately really get the job done? Although the above code is still just an elif
chain, it seems somewhat more complex than the approach I was contemplating.
While doing some C source generation for another project, I looked for some ways to do C preprocessing and analysis of defined macros in python. I couldn't find any nice ways to do what I wanted, so I ended up making my own.
This ingests source or header files and collects definitions for inspection or expansion.
It also emits the preprocessed source.
There are definitely some edge cases that I haven't handled, but its pretty reliable and works on realistic code bases.
E.g.
#define RAPIDJSON_NAMESPACE rapidjson
#define RAPIDJSON_NAMESPACE_BEGIN namespace rapidjson {
Like obviously this makes it easy to change the namespace by only touching one place, but I don't see why anyone would want to change that in the first place.
I'm trying to determine the number of flash blocks needed for reserving some flash space. I never get the "extra" +1 block. Is % not a valid operator? I don't get an error but the #else line never gets used. Or maybe it's something dumb.
#define QUOTIENT ((NUM_ITEMS*ITEM_SIZE)/FLASH_PAGE_SIZE)
#define REMAINDER ((NUM_ITEMS*ITEM_SIZE)%FLASH_PAGE_SIZE)
#if REMAINDER == 0
#define FLASH_BLOCKS QUOTIENT
#else
#define FLASH_BLOCKS (QUOTIENT+1)
#endif
// C program to illustrate macros
#include <stdio.h>
// Macro definition
#define AREA(l, b) (l * b)
// Driver Code
int main()
{
`// Given lengths l1 and l2`
`int l1 = 10, l2 = 5, area;`
`// Find the area using macros`
`area = AREA(l1, l2);`
`// Print the area`
`printf("Area of rectangle"`
`" is: %d",`
`area);`
`return 0;`
}
this is the macro program, i want preprocessed output of the program without using preprocessor extension,
we can exclude library the header files.
can anybody please help me with this how would i do so,
I want to see all the intermediate files (like one made by the preprocessor, assembler, etc) while compiling a C++ program. How to do that? Are there compiler flags to do that?
In order to help me write a lot of code needed to access MySql or to simplify import & export boilerplate or when I need xml, csv, json, etc. representations of my data, I have created structures that hold additional descriptions for internal structures.
One is RecFieldOffset. There I keep information about the type and position of a variable inside of a classic C structures together with its name so I can create an SQL WHERE clause, SQL bindings or whatever. Structures can even contain other structures but for the sake of my post here, I'll skip that part.
typedef struct _RecFieldOffset {
short fCType;
char *fCName;
short fCCount;
short fCLength; // for [x][y]
short fCFlags; // flags like skip, convert encoding,...
long fCOffset; // Calculate at init
} RecFieldOffset;
So for every struct I need to first create a typedef for that struct and then its description and yes, sometimes when I change the first part I forget the second step. This of course is bad and I wonder if I can somehow automate those two parts into one with some preprocessor macros.
Here's one example. A struct CustomerRecord and its description.
typedef struct { // Original C struct
long k_cust_cd;
char k_customer[42];
char k_adrdess[4][48];
...
char k_filler[190];
} CustomerRecord;
RecFieldOffset rfoCustomerRecord[] = { // Description
{ kTlong, "k_cust_cd", 0 },
{ kTchar, "k_customer", 42, },
{ kTchar, "k_adrdess", 4, 48, },
...
{ kTchar, "k_filler", 190, 0, kTFlagSkip },
{ 0, NULL, 0 }
};
What I need would be something like this in a header file that is included twice, but with different settings so it produces two different constructs for the compiler.
MY_START CustomerRecord
MY_LONG k_cust_cd, 0, 0, 0
MY_CHAR k_customer, 42, 0, 0
MY_CHAR. k_address, 4, 48, 0
...
MY_CHAR. k_filler, 190, 0, MY_SKIP
MY_END CustomerRecord
Any ideas? At least a reference to something similar or a good preprocessor tutorial that deals with more complex stuff.
I broadly don't really use the preprocessor much beyond include guards and basic replacement marcos, but I keep hearing that the preprocessor sucks and never explain why. The best I've heard is in the plan 9 papers where they say that #if "greatly complicates the preprocessor, is never necessary, and is usually abused."
Sort of a weird question, but I'm parsing through some c++/cuda code someone else has written and they call a function via #include "theFunction.h"
in the middle of their main, where theFunction header file is for all intents and purposes just runs the (cuda) function, e.g.
runFunction<<<BkGd, ThBk>>>(gFval, gIAval);
cudaDeviceSynchronize();
Is there any sensible reason I don't know about to do this via preprocessor directive or is the author just being unconventional here?
Hi, I have come across this weird fortran F90 file that contains sth like this:
#include "magic.h"
TYPE, BIND(C) :: foo(bar)
and inside magic.h there is:
#define foo(arg) foo_##arg
so the above line after the preprocessor is run should become
TYPE, BIND(C) :: foo_bar
if I use cpp -P to process the file this is exactly what I get but if I invoke the preprocessor via the gfortran interface I get this:
TYPE, BIND(C) :: foo_##bar
which is almost what I expected as the ## are mysteriously not removed. Any ideas are more than welcomed as this is driving me crazy!
Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.