Preprocessor problem

N

Nephi Immortal

How do I write correct preprocessor to test condition if I want to
select two different codes. I define SECURE_ON. The print is
supposed to report "SECURE_CPP is active." Why do it report
"SECURE_CPP is inactive." when I tell to turn on SECURE_ON before
SECURE_CPP is turned on automatically?
If there is no solution, what is another option?


// Test.h
#ifndef TEST_H
#define TEST_H

#ifdef SECURE_ON
#define SECURE_CPP
#else
#undef SECURE_CPP
#endif

void print();

#endif // TEST_H

// Test.cpp

#include <iostream>
#include “Test.h”

void print()
{
#ifdef SECURE_CPP
std::cout << "SECURE_CPP is active." << std::endl;
#else
std::cout << "SECURE_CPP is inactive." << std::endl;
#endif // SECURE_CPP
}

// main.cpp

#define SECURE_ON
#include “Test.h”

int main()
{
print();
return 0;
}
 
J

Jens Thoms Toerring

Nephi Immortal said:
How do I write correct preprocessor to test condition if I want to
select two different codes. I define SECURE_ON. The print is
supposed to report "SECURE_CPP is active." Why do it report
"SECURE_CPP is inactive." when I tell to turn on SECURE_ON before
SECURE_CPP is turned on automatically?
If there is no solution, what is another option?
// Test.h
#ifndef TEST_H
#define TEST_H
#ifdef SECURE_ON
#define SECURE_CPP
#else
#undef SECURE_CPP
#endif
void print();
#endif // TEST_H
// Test.cpp
#include <iostream>
#include “Test.hâ€
void print()
{
#ifdef SECURE_CPP
std::cout << "SECURE_CPP is active." << std::endl;
#else
std::cout << "SECURE_CPP is inactive." << std::endl;
#endif // SECURE_CPP
}
// main.cpp
#define SECURE_ON
#include “Test.hâ€
int main()
{
print();
return 0;
}

The problem is that main.cpp and Test.cpp both include Test.h
completely independently from each other. When the compiler
compiles main.cpp it will define 'SECURE_ON' since it's
asked to before including Test.h and this the will result
in 'SECURE_CPP' being set for the duration of the compila-
tion of main.cpp. When it's done with main.cpp the compiler
is started again to compile Test.cpp and then will know
nothing about what happend before. Thus, after including
<iostream> it will include Test.h, but without 'SECURE_ON'
being defined and thus 'SECURE_CPP' will not be defined by
Test.h.

Even though it might be a bit hidden by whatever development
environment you're using you should always be aware that
creating the executable from your two cpp files consists
of three distinct and independend steps:

a) compile manin.cpp
b) compile Test.cpp
c) link the resulting two object files into a single
executable

And a) and b) each invoke a new instance of the compiler
(or, one a multi-processor machine, may be run as two
instances of the compiler at the same time). Thus the
instance compiling main.cpp and that compiling Test.cpp
don't know anything about what the other is doing.

The simplest solution for your problem is to have a second
include file in which you define (or don't) 'SECURE_ON'
and have that included by Test.h. Or, alteratively, you
could pass it via a command line argument to both the in-
stances of the compiler, i.e. if you want to set 'SECURE_ON'
and you're using g++ just invoke the compiller with the
option '-DSECURE_ON' to have 'SECURE_ON' being defined.
And with Visual Studio I think it's '/DSECURE_ON' instead.

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top