preprocessor

#undef-ing in Practice?

I'm wondering about the practical use of #undef in C. I'm working through K&R, and am up to the preprocessor. Most of this was material I (more or less) understood, but something on page 90 (second edition) stuck out at me: Names may be undefined with #undef, usually to ensure that a routine is really a function, not a macro: ...

## in Macros

As mentioned in many of my previous questions, I'm working through K&R, and am currently into the preprocessor. One of the more interesting things -- something I never knew before from any of my prior attempts to learn C -- is the ## preprocessor operator. According to K&R: The preprocessor operator ## provides a way to concatenate...

VC6 setting additional include path using env vars?

I can't believe I am asking this... It has been a while since I used VC6. I set up what I thought was the correct way to include additional directories for include files (I removed another developer's hard coded paths) and put in something like: %MY_DIR%\include but this does not work. Am I missing something? Did VC6 not allow this...

Convert a preprocessor token to a string

I'm looking for a way to convert a preprocessor token to a string. Specifically, I've somewhere got: #define MAX_LEN 16 and I want to use it to prevent buffer overrun: char val[MAX_LEN+1]; // room for \0 sscanf(buf, "%"MAX_LEN"s", val); I'm open to other ways to accomplish the same thing, but standard library only. ...

Xcode Preprocessor Macros

In Xcode, I can edit my preprocessor macros in the project settings. I want to create a macro that refers to an environment variable. Basically, I want to be able to refer to $SRC_ROOT in my code. What I currently have in my macros is: SRC_ROOT=${SRC_ROOT} but it isn't working. ...

How do you invoke the Visual Studio Preprocessor from the command line?

I want to implement the solution using the pre-processor described here: http://stackoverflow.com/questions/100854/reuse-define-statement-from-h-file-in-c-code Bonus points if you can point me to the docs at MSDN. I'm having trouble finding them... ...

What's the best way to build variants of the same C/C++ application.

I have three closely related applications that are build from the same source code - let's say APP_A, APP_B, and APP_C. APP_C is a superset of APP_B which in turn is a superset of APP_A. So far I've been using a preprocessor define to specify the application being built, which has worked like this. // File: app_defines.h #define APP_A ...

What is a good reference documenting patterns of use of X-Macros in C (or possibly C++)?

A basic definition and example and a few references for "X-Macros" is given in this wikipedia entry on the C pre-processor: An X-Macro is a header file (commonly using a ".def" extension instead of the traditional ".h") that contains a list of similar macro calls (which can be referred to as "component macros"). What are s...

Is there a way to set the value of #define on runtime?

I wonder if there is a way to set the value of #define in run time. I assume that there is a query for Oracle specific and Sql Server specific at the code below. #define oracle // ... #if oracle // some code #else // some different code. #endif ...

C/C++ source file after preprocessing

Let's say I have a source file with many preprocessor directives . Is it possible to see how it looks after the preprocessor is done with it ? ...

Parsing C++ preprocessor #if statements

I have a C/C++ source file with conditional compilation. Before I ship it to customers I want to remove most of the #if statements, so that my customers do not need to worry about passing the right -D options to the compiler. I have this implemented and working in Python, but it only handles #ifdef and #ifndef statements properly. I n...

How to know (in GCC) when given macro/preprocessor symbol gets declared?

Suppose I have #define foo in various header files. It may expand to some different things. I would like to know (when compiling a .cc file) when a #define is encountered, to what it will expand, it which file it is and where it got included from. Is it possible? If not, are there any partial solutions that may help? Feel free to add c...

Prevent C preprocessor to do a specific macro subsitution

How can I tell the preprocessor not to replace a specific macro? The specific problem is the following: Windows header files define the GetMessage macro. My C++ header files with my API have a GetMessage method. I do not want to rename my method. But when using the API on Windows, including windows.h replaces my GetMessage method call ...

c# Pre-processor directive scope

Im looking to use: #define and #if to allow me to simulate potentially absent hardware during unit tests. What are the rules for useing the #define statements? i.e. what is its default scope? can I change the scope of the directive? ...

Writing a while loop in the C preprocessor

I am asking this question from an educational/hacking point of view, (I wouldn't really want to code like this). Is it possible to implement a while loop only using C preprocessor directives. I understand that macros cannot be expanded recursively, so how would this be accomplished? Thanks ...

How do I replace this preprocessor macro with a #include?

UPDATE: Obviously, you'd want to do this using templates or a base class rather than macros. Unfortunately for various reasons I can't use templates, or a base class. At the moment I am using a macro to define a bunch of fields and methods on various classes, like this: class Example { // Use FIELDS_AND_METHODS macro to define some...

How to define a preprocessor symbol in Xcode

Is it possible to set a symbol for conditional compilation by setting up properties in an Xcode project? My aim is to to create a symbol that is available to all files, without having to use import/include, so that a set of common classes can have a special behavior in some projects. Like the following, but with my own symbols. #if TAR...

What are the valid characters for macro names?

Are C-style macro names subject to the same naming rules as identifiers? After a compiler upgrade, it is now emitting this warning for a legacy application: warning #3649-D: white space is required between the macro name "CHAR_" and its replacement text #define CHAR_& 38 This line of code is defining an ASCII value c...

Namespaces in C

Is there a way to (ab)use the C preprocessor to emulate namespaces in C? I'm thinking something along these lines: #define NAMESPACE name_of_ns some_function() { some_other_function(); } This would get translated to: name_of_ns_some_function() { name_of_ns_some_other_function(); } ...

Replacements for the C preprocessor

I'm interested in using something other than the C preprocessor to preprocess my C and Objective-C source code. Are there good alternatives? An example would be something that allowed one to escape out into a python or perl snippet in the middle of C code, and where the snippet spit out C that is then compiled as normal. ...