Questions tagged [ifndef]

Is one of the basic C/C++ pre processor macros to include/exclude specific parts of the source code to be compiled. "ifndef" branch is true if a specific pre-processor macro is not defined.

There are various C/C++ pre processor macros to control the compilation process. Some of them are used to include/exclude specific parts of the source code from compilation. These are conditional macros like #IF, #IFDEF, #IFNDEF.

Example:

#IFDEF __DEBUG__
    printf("This is a debug binary");
#ELSE
    printf("This is a release binary");
#ENDIF

If __DEBUG__ is defined "This is a debug binary" will be printed to console.

60 questions
96
votes
7 answers

Why only define a macro if it's not already defined?

All across our C code base, I see every macro defined the following way: #ifndef BEEPTRIM_PITCH_RATE_DEGPS #define BEEPTRIM_PITCH_RATE_DEGPS 0.2f #endif #ifndef BEEPTRIM_ROLL_RATE_DEGPS #define BEEPTRIM_ROLL_RATE_DEGPS …
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
13
votes
2 answers

Conditional exclusion of code in Swift

I am trying to exclude some parts of a Swift file for a specific target. Yet I did not find any replacement of the #ifndef objective-c directive and moreover if I use a form of the kind: #if taxi_coops func pippo(){ …
Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75
6
votes
1 answer

Multiple definition of namespace function

I just can't get my head around why this won't compile. I have three files: main.cpp #include "expression.h" int main(int argc, char** argv) { return 0; } expression.h #ifndef _EXPRESSION_H #define _EXPRESSION_H namespace OP { char…
l. schwarz
  • 115
  • 2
  • 7
4
votes
2 answers

Conditional compilation in C and Delphi

The next pattern is common in C code: #ifndef SOMETHING #define SOMETHING #endif The pattern is possible in Delphi code too: {$IFNDEF SOMETHING} {$DEFINE SOMETHING} {$ENDIF} but it is not common - I have never seen it at all. If a Delphi code…
kludg
  • 27,213
  • 5
  • 67
  • 118
4
votes
2 answers

How do you enable "#ifndef/#endif" blocks in makefile builds?

I'm trying to enable debugging options in MuPDF. For some reason they have used #ifndef NDEBUG and #endif greying out code I want to use. I searched throughout the library but couldn't find any traces of NDEBUG defined anywhere. I've managed to work…
Some Noob Student
  • 14,186
  • 13
  • 65
  • 103
3
votes
2 answers

Do I check the existence of a macro function with or without the parens?

Before I define a macro function, I can check that it doesn't already exist (this avoids overwriting a previous definition). I can implement the check and definition like this: #ifndef MACRO(X) #define MACRO(X) FUNCTION(X) #endif Or like…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
2
votes
2 answers

Multiple definition of namespace::variable even using ifndef

I know I must be doing something wrong here. rank.h #ifndef RANK_H #define RANK_H namespace mmi { int chunk; void rank(int my_rank); } #endif rank.cpp #include "rank.h" namespace mmi { //do something with chunk } main.cpp #include "rank.h" int…
KcFnMi
  • 5,516
  • 10
  • 62
  • 136
2
votes
1 answer

Preprocessor arguments and compiling #ifndef #ifdef in C

Trying to apply a default value in my code when compiling the file. I need to set a defined key word to a certain value in my code. So when I compile the code and it doesn't receive any definition in the arguments of the compiler it will use my…
Cyberflow
  • 1,255
  • 5
  • 14
  • 24
2
votes
1 answer

Can #ifndef ignore method or variable duplications?

Consider the code. #ifndef FOO_H #define FOO_H //Code #endif Code can be following cases // Case 1: #define foo 0 // Case 2: void foo_method(){}; // Case 3: int foo; foo.h is included in many C files. When I compile only the case 1 is without…
sandy
  • 93
  • 1
  • 8
2
votes
0 answers

Typedef in header file not visible to another file that includes it

I am encountering the following problem in C: I declare a typedef for a struct in a headerfile ("mep.h") #ifndef MEP_H #define MEP_H typedef struct Mep_tag Mep; #endif I use another headerfile ("mep_types.h") that…
Constantine
  • 43
  • 1
  • 6
2
votes
1 answer

Using ifdef and ifndef directives

I'm trying to check whether a variable is defined using ifndef/ifdef, but I keep getting a not found error from the execution. I'm using GNU Make 3.81, and here is a snippet of what I have: all: _images $(call clean, .) …
Rubens
  • 14,478
  • 11
  • 63
  • 92
2
votes
4 answers

C++ trying to use #ifndef and #include statements

Ok, so I'm a HTML/Javascript/PHP professional, but I'm trying to learn C++. I'm still a newbie at it, and I have a C++ programming project that I have errors with. The file that contains int main() is 'football.cpp', and I have .h and .cpp files…
CalvinCopyright
  • 165
  • 2
  • 4
  • 10
1
vote
1 answer

Preprocessor command order

I know that when I have only one source file in C++, preprocessor commands are done in order they were written, but what if I have more than one source file? How is the decision made, which source file should be taken at first? I've written in both…
Huuulk99
  • 21
  • 3
1
vote
1 answer

Compilation error while including header file in compile command

I have two files main.c and header.c. main.c has some macro STR who value I want to define conditionally according to some #define in the file. Case 1: When I include header.c in main.c file, the program is working fine as shown…
Anupam
  • 428
  • 7
  • 15
1
vote
2 answers

#define c-preprocessor constants ... what did I wrong?

I am trying once more with arduino and create a small module just to gain fluency in the cpp sintaxe. I am trying to create a utility module with a static method and using a header constant to decide if I have to print the debug messages or not. But…
Marcio Barroso
  • 783
  • 1
  • 7
  • 21
1
2 3 4