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.
ifndef
60
questions
97
votes
7
answers
7k
views
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 ...
13
votes
2
answers
9k
views
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 ...
6
votes
1
answer
4k
views
Multiple definition of namespace function [duplicate]
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
...
4
votes
2
answers
2k
views
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 - ...
4
votes
2
answers
11k
views
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 ...
3
votes
2
answers
457
views
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)
#...
3
votes
2
answers
2k
views
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 ...
2
votes
4
answers
17k
views
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() ...
2
votes
1
answer
3k
views
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 ...
2
votes
1
answer
137
views
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 ...
2
votes
1
answer
16k
views
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: ...
2
votes
0
answers
2k
views
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;
<other stuff declared here>
#...
1
vote
3
answers
306
views
Include Guards in C
I have 2 header files that have to include one other.
config.h:
#ifndef CONFIG
#define CONFIG
#include "debug.h"
typedef struct Config_t {
/* some stuff */
} Config;
#endif
debug.h
#...
1
vote
4
answers
1k
views
C++ #ifndef TOKEN #define TOKEN
There has been a few cases where I've seen preprocessor code like the following:
#ifndef TOKEN
#define TOKEN
#endif
To me, it seems that in this situation (I'm aware of it's use when wrapped around ...
1
vote
1
answer
250
views
WHY "already defined"?
Please suggest me a hint over here:
class UIClass
{
public:
UIClass::UIClass();
};
#ifndef __PLATFORM__
#define __PLATFORM__
UIClass Platform;
#else
extern UIClass Platform;
#endif
I'm ...
1
vote
2
answers
884
views
Multiple #ifndef statements - which one gets applied
Say I have five files: main.c, sample1.c, sample1.h, sample2.c and sample2.h, where in each of these files DEBUG_PRINTS is defined as follows:
#ifndef DEBUG_PRINTS
#define DEBUG_PRINTS 0
#endif
and ...
1
vote
1
answer
4k
views
possible to use #ifndef on a function name? [duplicate]
I'm working with MicroPython and their header files use a ton of
#ifndef function_name
void function_name(const char *str, size_t len);
#endif
Using both an online C and C++ compiler, I tried the ...
1
vote
1
answer
505
views
How to combine ifndef win32 with ifndef iOS [duplicate]
I have a C++ code section that I want to exclude it from building iOS.
How should I do it?
Follow up question:
If I already have an #ifndef WIN32. I want to combine exclusion of WIN32 with iOS is it ...
1
vote
2
answers
109
views
#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 ...
1
vote
2
answers
635
views
Unexpected #ifndef, No Matching Function Call (Linked List C++)
Hi i am currently working on a linked list project but am receiving a few errors while doing so that I can't seem to solve. The first error i am getting is an undetermined #ifndef. What im confused ...
1
vote
2
answers
875
views
Include Guards and #ifndef #define Preprocessing Statements
If I do the following:
dConst.hpp
const int POWER_LEVEL = 9001;
genPower.hpp
#include "dConst.hpp"
#ifndef GENPOWER_HPP
#define GENPOWER_HPP
const int GENERATOR[1] = { POWER_LEVEL };
#endif
I ...
1
vote
1
answer
4k
views
Multiple include error
I am new to C++ and am trying to include two .h files I made. The includes access each other so depending on the order I include them in one fails or the other. Because of this I know that the only ...
1
vote
1
answer
897
views
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 ...
1
vote
2
answers
2k
views
ifndef, define & direct assignment of constants
I am just thinking of the difference between below methods, while defining constants:
Method1:
Create a header file to define all the constants, using include guard:
#ifndef c1
#define c1 @"...
1
vote
1
answer
33
views
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 ...
1
vote
2
answers
350
views
Using #ifndef breaks node-gyp module
I'm writing a node.js module using C++ and node-gyp but when I fix all the errors, like in this question, which included getting rid of redundant declarations by adding
#ifndef ...
0
votes
2
answers
216
views
Redefinition error when using ifndef when alias is defined like "const int alias = variable" instead of #define
I defined const UInt8 HE = he; inside namespace Ports in ports.h. Then I included it in ports_logic.h and in ports_logic.h, I have the following code inside namespace Ports
#ifndef HP
const UInt8 HP = ...
0
votes
2
answers
415
views
How does the preprocessor know to translate HEADER_H to header.h?
Per this question, it seems there is some flexibility to how you can write that--
#ifndef _HEADER_H
or:
#ifndef __HEADER___H__
etc. It's not set in stone.
But I don't understand why we're ...
0
votes
2
answers
7k
views
Error: #if[n]def expected an identifier
Originally my code was:
#ifndef 2DO_H
#define 2DO_H
int ReadNumber();
void WriteAnswer(int Nsumber1, int Number2);
#endif
However I was getting an error #if[n]def expected an identifier. ...
0
votes
3
answers
179
views
Why should avoid redundant declarations in C++?
I am learning multiple file compilation in C++ and found practice like this:
#ifndef MY_LIB_H
#define MY_LIB_H
void func(int a, int b);
#endif
Some people say that this practice is adopted to avoid ...
0
votes
2
answers
606
views
ifndef isn't working; I have a header file included twice [duplicate]
My Ifndef isn't working. I have MapData.h included twice in both: Generate.cpp and Risky Strats.cpp
MapData.h:
#ifndef MAPDATA
#define MAPDATA
#include <iostream>
#include <vector>
class ...
0
votes
1
answer
88
views
Confusion with Preprocessor Directives
I have three files
File "grandparent.h"
#ifndef GRANDPARENT_H
#define GRANDPARENT_H
struct foo {
int member;
};
#endif /* GRANDPARENT_H */
File "parent.h"
#include "grandparent.h"
File "...
0
votes
1
answer
4k
views
Define statement expected a declaration C++
Okay, so I have narrowed the problem having to do with the #ifndef and/or the #define keywords.
I have 2 other .h files and the only difference between the ones with no errors and the ones without is ...
0
votes
2
answers
259
views
ifndef with multiple AND conditions in makefile gnu
How do I ensure that make command has the parameter ENV passed in it?
Eg: make destroy ENV=test if ENV is not passed I should throw error.
Code:
ENV ?=prod
ifndef ENV
$(error ENV is not defined, ...
0
votes
1
answer
2k
views
C++ two header files include each other
There are three .h files
A.h:
#ifndef __A_H__
#define __A_H__
#include"Card.h"
#include"B.h"
struct A{
Card card;
.....
};
void getCards(A *a, int num);
#endif
B.h
#ifndef __B_H__
#...
0
votes
1
answer
223
views
Having trouble with #ifndef in C++
I am creating a software for Raspberry pi using WiringPi. The problem is that WiringPi will fail if it does not detect a Raspberry Pi. So if I want to do some unit testing without using an actual ...
0
votes
1
answer
1k
views
#ifndef why to use an other name than the class name?
Classes declarations usually look like that:
#ifndef MY_CLASS_20141116
#define MY_CLASS_20141116
...
class MyClass
{
...
}
#endif
My question is, why to not use the class name instead of ...
0
votes
1
answer
724
views
User Defined Build Settings in Test Targets?
i have the following code in my unit tests:
#ifndef SERVER_TEST
NSLog(@"\n\n!!!--- YOU ARE RUNNING TESTS IN STUB MODE ---!!!\n\n!!!--- Server Responses will be stubbed ---!!!\n\n");
#else
...
0
votes
3
answers
617
views
Preprocessor #ifndef
Assume I have a.h which includes the following:
<stdbool.h>
<stddef.h>
<stdin.h>
Assume I also have b.h which also includes <stdbool.h>. If a.h has the #ifndef preprocessor ...
0
votes
1
answer
40
views
Use of Macro for static declaration
While reviewing C code that has been migrated from AIX to Cygwin 64-bit, I came across the following snippet. Writing only "static" within the "#ifndef" directive doesn't make ...
0
votes
1
answer
126
views
Using ifndef in the makefile inside a variable defined by export
I want to insert ifndef in the makefile inside export
And it seems that there is no possibility
export LINKER_INCLUDE_FILES:=$(BASE_PATH)/build/one/bmw/r4_0_cantata/Deploy/src/platform/one/r4/cr4_cpu....
0
votes
1
answer
119
views
about #ifndef and macro_function
i have changed complier from msvc to mingw,
macro function : _countof was not inluded in mingw <stblib.h> or <stdio.h>
#define _countof(array) (sizeof(array) / sizeof(array[0]))
maybe it'...
0
votes
1
answer
90
views
Conditional Compile of const static arrays
I am trying to create an error enum and associated text descriptors aligned in the same file. I have a system.cpp file that contains the following:
#define SYSTEMCODE
#include "myerrors.h"
The file ...
0
votes
1
answer
2k
views
Redefined symbol in multiple c code with #ifndef directive
I have a stupid problem and I don't see where it comes from. I took care of using #ifndef directive to make sure all my #include are not redefined. Sadly for three of them that's happening. Here my ...
0
votes
1
answer
482
views
How #ifndef works in different files
So I was trying to include the libraries I have declared in my main.cpp to my header.h
//In my main.cpp
#include <cmath>
#include <deque>
#include <vector>
using namespace std;
//...
0
votes
0
answers
630
views
#ifndef error : need to update includepath
My header file "broker.h" is
#ifndef _BROKER_
#define _BROKER_
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <list>
#...
0
votes
1
answer
106
views
#ifndef not letting my files see what's in the header (C++)
So I created a Binary tree class and I want to separate the class definition from its member function definition by putting the class def in the .h file and the function definitions in the .cpp file. ...
0
votes
0
answers
37
views
Why does the C(++) preprocessor allow be to define a macro but not check if it's not defined?
If I have a simple statement like this:
#ifndef A(B)
#define A(B) B
#endif
I get a compilation error...
error: extra tokens at end of #ifndef directive [-Werror]
#ifndef A(B)
^
But if I ...
0
votes
1
answer
457
views
cpp - why didn't guardian protect me from multiple definitions? [duplicate]
I'm more into JAVA, but when HI-Perf is in the requirements list C/C++ must come on the table. And so it did. And, as expected, I'd stumbled upon something I cannot understand and cannot dig out in SO....
0
votes
0
answers
88
views
TUIO #ifndef WIN32 gets ignored
I am using the TUIO framework in Visual Studio 2013. When I build, the error 'pthread.h no such file or directory' appears. It seems TUIO runs on Linux and Windows and the #ifndef WIN32 checks for the ...