Preprocessor problem

Discussion in 'C++' started by Nephi Immortal, Dec 9, 2011.

  1. 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;
    }
    Nephi Immortal, Dec 9, 2011
    #1
    1. Advertising

  2. Nephi Immortal <> 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 ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Dec 9, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cronus
    Replies:
    1
    Views:
    658
    Paul Mensonides
    Jul 15, 2004
  2. Johnny Willemsen

    Preprocessor problem with Visual Age 6

    Johnny Willemsen, May 2, 2005, in forum: C++
    Replies:
    0
    Views:
    340
    Johnny Willemsen
    May 2, 2005
  3. onkar

    preprocessor problem

    onkar, Jun 6, 2006, in forum: C Programming
    Replies:
    3
    Views:
    359
    Suman
    Jun 6, 2006
  4. Henry Townsend

    token pasting problem in K&R preprocessor

    Henry Townsend, Jun 19, 2006, in forum: C Programming
    Replies:
    13
    Views:
    648
  5. Subra
    Replies:
    7
    Views:
    430
    Subra
    Jul 4, 2007
Loading...

Share This Page