Execute C Macro Function At Compile Time - c

Is it possible to run a c macro function at compile time.
for example writing something in a file each time code is compiled.

Yes; No.
The macros do execute at compile time but there isn't much you can do directly with them other than mix text into the code.
Now, taking the software tools approach that unix pioneered (after all) you could conditionally generate output with #warning and then catch this with some script via a pipe.
Then that script could do stuff.
But, you probably wouldn't want to do that. Once you are running a script you could just have that script do whatever you want. Also, #error and #warning don't macro-expand the error or warning text, so using them for I/O is problematic.
This is obvious, I suppose, but how about using Ruby, Python, or the shell to script some macro processing?

Macros in C are strictly a text substitution tool. The output of the preprocessor is a preprocessed file.
So no, you can't do additional tasks like that from the preprocessor.
If you really want to perform certain tasks at compile time, that's what make is for.

Related

Is it possible to see the macros of a compiled C program?

I am trying to learn C and I have this C file that I want view the macros of. Is there a tool to view the macros of the compiled C file.
No. That's literally impossible.
The preprocessor is a textual replacement that happens before the main compile pass. There is no difference between using a macro and putting the code the macro expands to in its place.*
*Ignoring the debugger output. But even then you can do it if you know the right #pragma to tell it the file and line number.
They're always defined in the header file(s) that you've imported with #include, or that those files in turn #include.
This may involve a lot of digging. It may involve going into files that make no sense to you because they're not written for casual inspection.
Any macros of any importance are usually documented. They may use other more complex implementation-specific macros that you shouldn't concern yourself with ordinarily, but if you're curious how they work the source is all there.
That being said, this is only relevant if you have the source and more specifically a complete build environment. Once compiled all these definitions, like the source itself, do not appear in the executable and cannot be inferred directly from the executable, especially not a release build.
Unlike Java or C#, C compiles directly to machine code so there's no way to easily reverse that back to the source. There are "decompilers" that try, but they can only really guess as to the original source. VM-based languages like Java and C# only lightly compile the code, sot here are a lot of hints as to how that code was generated and reversing it is an easier process.

How to trace how/where the printf is disabled?

My product embeds TCL VM to run TCL script. We basically take the TCL 8.4 source and integrate it to our product, the whole product is programmed in C.
Now I need to debug some issue and best I can have some insight about TCL VM at run time. So I add some printf to the TCL source, but I cannot see any print out. Note that the printf added to our side of the code works as expected.
This leads me to suspect that somewhere in TCL the printf is disabled.
I see the following code snippet in TCL source:
#ifdef TCL_COMPILE_DEBUG
fprintf(stdout, " Starting stack top=%d\n", eePtr->stackTop);
fflush(stdout);
#endif
I rebuild TCL by enabling the TCL_COMPILE_DEBUG, still I cannot see print out.
Any suggestion how I should proceed from here?
It seems unlikely that the standard library's fprintf() is disabled. Instead, I see three main alternatives:
The fprintf() you have added is never being called. That could be because it's in the wrong place, because conditional compilation directives cause it to be omitted, or perhaps for some other reason.
The fprintf() being called is not the standard library's, and it does not do what you expect. It might instead be a local function in the TCL VM's code, or the TCL VM might #define it to something else altogether. Depending on exactly how you integrate TCL into your larger code, these possibilities might be limited in scope to just TCL.
stdout does not mean what you think it does inside the TCL code. This would almost surely be as a result of it being #defined to something else, for some reason important to the TCL VM. In that case, there might or might not be a way to get the real stdout in that scope.
I'd suggest you grep the TCL code you have integrated for the fprintf and stdout symbols, to look for macro definitions and alternative implementations. It would also be worthwhile to check the preprocessor output to make sure your call is still there (and is still the call you expected). If you are compiling with GCC, then you can preprocess your sources without compiling the result via gcc -E.

Expanding a C macro selectively [duplicate]

I was wondering if it is possible, and if yes how, can I run a C preprocessor, like cpp, on a
C++ source file and only process the conditional directives #if #endif etc. I would like other
directives to stay intact in the output file.
I'm doing some analysis on C# code and there is no C# pre-processor. My idea is to run a C preprocessor on C# file and process only conditionals. This way for example, the #region directive, will stay
in the file, but cpp appears to remove #region.
You might be looking for a tool like coan:
Coan is a software engineering tool for analysing preprocessor-based configurations of C or C++ source code. Its principal use is to simplify a body of source code by eliminating any parts that are redundant with respect to a specified configuration.
It's precisely designed to process #if and #ifdef preprocessor lines, and remove code accordingly, but it has a lot of other possible uses.
The linux unifdef command does what you want:
http://linux.die.net/man/1/unifdef
Even if you're not on linux, there is source available on the web.
BTW, this is a duplicate of another question: Way to omit undefined preprocessor branches by default with unifdef?
Oh, this is the same task as I had in the past. I've tried cpp unifdef and coan tools - all of them stumbled upon special C# preprocessor things like #region. In the end I've decided to make my own one:
https://github.com/gaDZella/undefine.
The tool has a pretty simple set of options compared to the mentioned cpp tools but it is fully compatible with C# preprocessor syntax.
You can use g++ -E option to stop after preprocessing stage
-E -> stop after the preprocessing stage.The output is in the form of preprocessed source code, which is sent to the standard output

How to find compiler errors in C macros?

We have a code base that relies on lots of generated code generated by C macros.
If something goes wrong and there is a error or a warning, the compiler points at the line of the first macro expansion without telling more about where it went wrong inside the expanded code. I my particular case they are those /analyze warnings in Visual Studio.
Are there any tricks and tips that help find problems in complex preprocessor macros?
EDIT:
If you wonder why this code base have complex macros.
This is an emulator project where the decoding phase and execution phase is separated. For example instead of finding out during the execution of each instruction what addressing mode or operand size, etc is used, we generate a function for each combination with a DEFINE_INSTRUCTION macro which in turn generate the functions for all combinations. And chain these functions.
idea: dont ;) don't use macros that are complicated as you loose a lot of IDE support / compiler support
=> if you have such macros, refactor them into functions... maybe even inline functions
but seriously. to help you with the bad macros you're stuck with: As TripeHound said, there are flags to 'compile' C files only to the stage of preprocessed C files --
On the command line, clang -E foo.m will show you the preprocessed output.

Preprocessor-like substitution into a parser

I am making a parser currently which aims to be able to input data in a program.
The syntax used is greatly inspired from C.
I would enjoy to reproduct a kind of preprocessor inline substitution into it.
for example
#define HELLO ((variable1 + variable2 + variable3))
int variable1 = 37;
int variable2 = 82;
int variable3 = 928;
Thing is... I'm actually using C. I'm also using standard functions from stdio.h to parse through my files.
So... what techniques I could use to make this work correctly and efficiently?
Does the standard compilers substitute the text by re-copying the stream buffer and making the substitution there as the re-copying occurs or what? Is there more efficient techniques?
I guess we say preprocessor because it first substitutes everything until theres no preproc directives (recursive approach maybe?), and then, it starts doing the real compile job?
Excuse my lack of knowledge!
Thanks!
No, modern C compilers don't implement the preprocessor as a text processor, but they have the different compiler phases (preprocessing being one of them) tangled. This is particularly important for the efficiency of the compiler itself and to be able to track errors back into the original source code.
Also implementing a preprocessor by yourself is a tedious task. Think twice before you start such a project.
Yes, you are right about preprocessors. It has the job of bringing together all files which are requires for the execution of the program to 1 file for eg. stdio.h. Then it allows the compiler to compile the program. The file you want to compile is given as argument to the compiler and the techniques used by the compiler may vary according to the os and the compiler itself
The C preprocessor works on tokens not text. In particular, macro expansion cannot contain preprocessor directives. Other preprocessors, such as m4, work differently.

Resources