Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Preprocessor problem (http://www.velocityreviews.com/forums/t806759-preprocessor-problem.html)

Nephi Immortal 12-09-2011 04:07 AM

Preprocessor problem
 
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;
}

Jens Thoms Toerring 12-09-2011 08:50 AM

Re: Preprocessor problem
 
Nephi Immortal <immortalnephi@gmail.com> wrote:
> 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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de


All times are GMT. The time now is 11:22 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.