preprocessor

How do you create a debug only function that takes a variable argument list? Like printf()

I'd like to make a debug logging function with the same parameters as printf. But one that can be removed by the pre-processor during optimized builds. For example: Debug_Print("Warning: value %d > 3!\n", value); I've looked at variadic macros but those aren't available on all platforms. gcc supports them msvc does not. ...

What's a 'null defined macro'?

I'm learning objective-C and Cocoa. In the Apple tutorial I'm working through there's a side note that says: IBOutlet is a null-defined macro, which the C preprocessor removes at compile time. I'm curious - what's a null-defined macro? Cheers Ben ...

C# Preprocessor

While the C# spec does include a pre-processor and basic directives (#define, #if, etc), the language does not have the same flexible pre-processor found in languages such as C/C++. I believe the lack of such a flexible pre-processor was a design decision made by Anders Hejlsberg (although, unfortunately, I can't find reference to this n...

#line and jump to line

Do any editors honer C #line directives with regards to goto line features? Context: I'm working on a code generator and need to jump to a line of the output but the line is specified relative to the the #line directives I'm adding. I can drop them but then finding the input line is even a worse pain ...

How should I write code with unique sections for different versions of .NET

My source code needs to support both .NET version 1.1 and 2.0 ... how do I test for the different versions & what is the best way to deal with this situation. I'm wondering if I should have the two sections of code inline, in separate classes, methods etc. What do you think? ...

When are C++ macros beneficial?

The C preprocessor is justifiably feared and shunned by the C++ community. In-lined functions, consts and templates are usually a safer and superior alternative to a #define. The following macro: #define SUCCEEDED(hr) ((HRESULT)(hr) >= 0) is in no way superior to the type safe: inline bool succeeded(int hr) { return hr >= 0; } Bu...

Reuse define statement from .h file in C# code

Hi! I have C++ project (VS2005) which includes header file with version number in #define directive. Now I need to include exactly the same number in twin C# project. What is the best way to do it? I'm thinking about including this file as a resource, then parse it at a runtime with regex to recover version number, but maybe there's a b...

What is the best way to only include certain libraries on certain operating systems in c/c++?

When writing an app that one wants to have compile on mac, linux and windows, what is the best way of managing the different libraries that will need to be included on the various operating systems. For example, using the glut opengl toolkit requires different includes on each operating system. ...

Making something both a C identifier and a string?

Say you want to generate a matched list of identifiers and strings enum { NAME_ONE, NAME_TWO, NAME_THREE }; myFunction(NAME_ONE, "NAME_ONE"); myFunction(NAME_TWO, "NAME_TWO"); myFunction(NAME_THREE, "NAME_THREE"); ..without repeating yourself, and without auto-generating the code, using C/C++ macros Initial guess: You could add an ...

Difference between Enum and Define Statements

What's the difference between using a define statement and an enum statement in C/C++? (and is there any difference when using them with either C or C++?) For example, when should one use enum {BUFFER = 1234}; over #define BUFFER 1234 Thanks ...

How to escape an underscore in a C preprocessor token?

The following snippet is supposed to take the value of PROJECT (defined in the Makefile) and create an include file name. For example, if PROJECT=classifier, then it should at the end generate classifier_ir.h for PROJECTINCSTR I find that this code works as long as I am not trying to use an underscore in the suffix. However the use of t...

mangling __FILE__ and __LINE__ in code for quoting?

Is there a way to get the C/C++ preprocessor or a template or such to mangle/hash the __FILE__ and __LINE__ and perhaps some other external input like a build-number into a single short number that can be quoted in logs or error messages? (The intention would be to be able to reverse it (to a list of candidates if its lossy) when needed...

How do I check OS with a preprocessor directive?

I need my code to do different things, based on the operating system it gets compiled on. I'm looking for something like this: #ifOSisWindows   //define something for Windows #else   //define it for a Unix machine #endif Is there a way to do this? Is there a better way to do the same thing? ...

Can the C preprocessor be used to tell if a file exists?

I have a very large codebase (read: thousands of modules) that has code shared across numerous projects that all run on different operating systems with different c++ compilers. Needless to say, maintaining the build process can be quite a chore. There are several places in the codebase where it would clean up the code substantially if...

Easy way to use variables of enum types as string in C?

Here's what I am trying to do: typedef enum { ONE, TWO, THREE } Numbers; I am trying to write a function that would do a switch case similar to the following: char num_str[10]; int process_numbers_str(Numbers num) { switch(num) { case ONE: case TWO: case THREE: { strcpy(num_str, num); //some way to get the ...

How can I detect if I'm compiling for a 64bits architecture in C++

In a c++ function I need the compiler to choose a different block if it is compiling for a 64 bit architecture. I know a way to do it for MSVC++ and g++, so i'll post it as an answer. However I would like to know if there is a better way (more elegant that would work for all compilers/all 64 bits architectures). If there is not a better...

Portability of #warning preprocessor directive

I know that the #warning directive is not standard C/C++, but several compilers support it, including gcc/g++. But for those that don't support it, will they silently ignore it or will it result in a compile failure? In other words, can I safely use it in my project without breaking the build for compilers that don't support it? ...

C++ #include semantics

This is a multiple question for the same pre-processing instruction. 1 - <> or "" ? Appart from the info found in the MSDN: http://msdn.microsoft.com/en-us/library/36k2cdd4(VS.80).aspx 1.a: What are the differences between the two notations? 1.b: Do all compilers implement them the same way? 1.c: When would you use the <>, and when w...

#warning directive in VB.net

I know the #warning directive does not exist in vb.net... is there anything like it? I want to be able to throw messages (warnings) at compiler time. ...

How to make a char string from a C macro's value?

For example, how to avoid writing the 'func_name' twice? #ifndef TEST_FUN # define TEST_FUN func_name # define TEST_FUN_NAME "func_name" #endif I'd like to follow the Single Point of Truth rule. Version of C preprocessor: $ cpp --version cpp (GCC) 4.1.2 20070626 (Red Hat 4.1.2-14) ...