Questions tagged [preprocessor]

A program that processes input data to produce output that is used as input to another program. Use this tag for questions about an unspecified pre-processor. If there is a specific tag for the pre-processor you should use that. Consider using [tag:c-preprocessor], [tag:boost-preprocessor], [tag:oracle-pro-c], [tag:css-preprocessor], [tag:karma-babel-preprocessor], [tag:m4]

A preprocessor is a program that processes its input data to produce output that is used as input to another program according to a given set of rules. Typically a preprocessor is used to perform a phase of translation of source code before the next step of compilation.

Preprocessors may be categorised as lexical, syntactic or general purpose. The most familiar example of a lexical preprocessor is the which has its own tag. Although also used as a general purpose preprocessor, the C preprocessor lacks some of the features of more general macro processors such as .

When a pre-processor is used to generate further programs it is appropriate to use .

1834 questions
869
votes
19 answers

#ifdef replacement in the Swift language

In C/C++/Objective C you can define a macro using compiler preprocessors. Moreover, you can include/exclude some parts of code using compiler preprocessors. #ifdef DEBUG // Debug-only code #endif Is there a similar solution in Swift?
mxg
  • 20,946
  • 12
  • 59
  • 80
494
votes
8 answers

#if DEBUG vs. Conditional("DEBUG")

Which is better to use, and why, on a large project: #if DEBUG public void SetPrivateValue(int value) { ... } #endif or [System.Diagnostics.Conditional("DEBUG")] public void SetPrivateValue(int value) { ... }
Lucas B
  • 11,793
  • 5
  • 37
  • 50
279
votes
12 answers

How to determine whether code is running in DEBUG / RELEASE build?

I am making an app that processes sensitive credit card data. If my code is running in debug mode I want to log this data to the console and make some file dumps. However on the final appstore version (ie when it is running in release mode) it is…
P i
  • 29,020
  • 36
  • 159
  • 267
261
votes
10 answers

Preprocessor directives in Razor

I am writing my first Razor page today, and can't figure out how to enter #if debug ... #else ... #endif How can I do that in Razor?
mamu
  • 12,184
  • 19
  • 69
  • 92
212
votes
31 answers

__FILE__ macro shows full path

The standard predefined macro __FILE__ available in C shows the full path to the file. Is there any way to shorten the path and get just the filename? I mean instead of /full/path/to/file.c I see to/file.c or file.c
mahmood
  • 23,197
  • 49
  • 147
  • 242
176
votes
70 answers

What is the worst real-world macros/pre-processor abuse you've ever come across?

What is the worst real-world macros/pre-processor abuse you've ever come across (please no contrived IOCCC answers *haha*)? Please add a short snippet or story if it is really entertaining. The goal is to teach something instead of always telling…
Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
173
votes
37 answers

How to convert an enum type variable to a string?

How to make printf to show the values of variables which are of an enum type? For instance: typedef enum {Linux, Apple, Windows} OS_type; OS_type myOS = Linux; and what I need is something like printenum(OS_type, "My OS is %s", myOS); which must…
psihodelia
  • 29,566
  • 35
  • 108
  • 157
153
votes
6 answers

Can gcc output C code after preprocessing?

I'm using an open source library which seems to have lots of preprocessing directives to support many languages other than C. So that I can study what the library is doing I'd like to see the C code that I'm compiling after preprocessing, more like…
LGTrader
  • 2,349
  • 4
  • 23
  • 29
141
votes
1 answer

How to check release / debug builds using cfg in Rust?

With the C pre-processor it's common to do, #if defined(NDEBUG) // release build #endif #if defined(DEBUG) // debug build #endif Cargo's rough equivalents are: cargo build --release for release. cargo build for debug. How would Rust's…
ideasman42
  • 42,413
  • 44
  • 197
  • 320
128
votes
4 answers

Swift: how to use PREPROCESSOR Flags (like `#if DEBUG`) to implement API keys?

In Objective-C it was sometimes useful to use static string constants to define alternate API keys (for example to differentiate between RELEASE and DEBUG keys for analytics packages, like MixPanel, Flurry or Crashlytics): #if DEBUG static NSString…
cleverbit
  • 5,514
  • 5
  • 28
  • 38
110
votes
5 answers

"Debug only" code that should run only when "turned on"

I would like to add some C# "debug only" code that only runs if the person debugging requests it. In C++, I used to do something similar to the following: void foo() { // ... #ifdef DEBUG static bool s_bDoDebugOnlyCode = false; if…
Matt Smith
  • 17,026
  • 7
  • 53
  • 103
92
votes
18 answers

Xcode 4 can't locate public header files from static library dependency

Alternate titles to aid search Xcode can't find header Missing .h in Xcode Xcode .h file not found lexical or preprocessor issue file not found I'm working on an iOS application project which came from Xcode 3. I have now moved to Xcode 4 my…
Richard Stelling
  • 25,607
  • 27
  • 108
  • 188
83
votes
9 answers

#define in Java

I'm beginning to program in Java and I'm wondering if the equivalent to the C++ #define exists. A quick search of google says that it doesn't, but could anyone tell me if something similar exists in Java? I'm trying to make my code more readable.…
Meir
  • 12,285
  • 19
  • 58
  • 70
78
votes
9 answers

How would you do the equivalent of preprocessor directives in Python?

Is there a way to do the following preprocessor directives in Python? #if DEBUG < do some code > #else < do some other code > #endif
intrepion
  • 38,099
  • 4
  • 24
  • 21
72
votes
9 answers

Managing highly repetitive code and documentation in Java

Highly repetitive code is generally a bad thing, and there are design patterns that can help minimize this. However, sometimes it's simply inevitable due to the constraints of the language itself. Take the following example from…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
1
2 3
99 100