locked
first #define is undefined RRS feed

  • Question

  • Windows 10, Visual Studio 2012, C++

    I have used macros as little as possible.  Now I create my first program that can be compiled on Windows and on Linux Centos.  Begin with one source code file program that runs and add one define to get this.

        // enable one at a time as appropriate
    #define VISUAL_STUDIO_2012
    // #define CENTOS_7
    
        // stdafx must be first in windows so go ahead and get it and windows.h now
    #ifdef VISUAL_STUDIO_2012
    #include "stdafx.h"
    #include <windows.h>
    #endif
    
    

    The error code is

    Warning 1 warning C4603: 'VISUAL_STUDIO_2012' : macro is not defined or definition is different after

    precompiled header use D:\CODE_TESTS\STL_Comparisons\STL_Comparisons\STL_Comparisons.cpp 4 1

    STL_Comparisons

    When the code is: #define VISUAL_STUDIO_2012

    I flat out do not understand how the compiler can claim that its not defined. I tried adding a prefix in

    case VISUAL_STUDIO_2012 is already defined somewhere within VS but it made no difference. This is so simple I

    cannot understand how it can be an error.

    I see that note about being different after precompiled header, but as I see it, stdafx.h cannot be present for

    Linux but it must be first for Windows. I don't know how else this might be done.

    Sunday, May 27, 2018 3:31 AM

Answers

  • The key here, I believe, is that you can't have ANYTHING before the include of your precompiled header.  If your stdafx.cpp did not have VISUAL_STUDIO_2012 defined, then the pre-compiled snapshot would not include that definition, and that would cause this error.  I don't think you can even put the include of <stdafx.h> inside an ifdef.  You'll need to have the ifdef inside stdafx.h.

    There are lots of solutions.  You don't need to define your own symbols for this.  Every operating system has its own predefined symbols.  On Linux, you can use "#ifdef __linux".  On Windows, you can do "#ifdef _WIN32" or even "#ifdef MSC_VER".


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    • Marked as answer by bkelly77 Sunday, May 27, 2018 10:13 PM
    Sunday, May 27, 2018 6:15 AM
  • If you don't mind the potential affect to compile time, why don't you just disable the precompiled headers in Visual Studio? It isn't like you need them.

    Precompiled headers were introduced as a way to speed up compile times due to large amounts of include files, but the way that they were done was quite simplistic. First you compile an empty source file with the precompiled header included, use this to save the parse tree for that header.

    Second, when the compiler includes the precompiled header, it will just dump the saved parse tree at the start of the module and use it as a starting point. Because the pch is used as a base, nothing can exist prior to it. This is why the compiler basically ignores everything before the include.

    Anyway, to disable the precompiled headers, delete stdafx.cpp and stdafx.h, then go to the project properties. If you set Precompiled Header to Not Using Precompiled Headers:

    then the compiler won't ignore everything before the include of stdafx.h. You should also delete the #include "stdafx.h" line too.

    If you don't want to want to remove the precompiled headers, then put the #ifdef inside the precompiled header file. This means that you will still have to include it on Linux systems.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Marked as answer by bkelly77 Monday, May 28, 2018 12:21 AM
    Sunday, May 27, 2018 11:18 PM

All replies

  • The error message does not claim the macro is not defined.  It claims the macro is not defined OR it is changed in a precompiled header.

    What is in your stdafx.h?

    Sunday, May 27, 2018 4:09 AM
  • stdafx.h is completely unchanged.  This is a one source file, one project solution.  Here are its complete contents with a few blank lines removed for brevity.

    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently
    //
    #pragma once
    #include "targetver.h"
    #include <stdio.h>
    #include <tchar.h>
    // TODO: reference additional headers your program requires here
    Why might the precompiled header change it?



    • Edited by bkelly77 Sunday, May 27, 2018 4:18 AM
    Sunday, May 27, 2018 4:16 AM
  • The key here, I believe, is that you can't have ANYTHING before the include of your precompiled header.  If your stdafx.cpp did not have VISUAL_STUDIO_2012 defined, then the pre-compiled snapshot would not include that definition, and that would cause this error.  I don't think you can even put the include of <stdafx.h> inside an ifdef.  You'll need to have the ifdef inside stdafx.h.

    There are lots of solutions.  You don't need to define your own symbols for this.  Every operating system has its own predefined symbols.  On Linux, you can use "#ifdef __linux".  On Windows, you can do "#ifdef _WIN32" or even "#ifdef MSC_VER".


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    • Marked as answer by bkelly77 Sunday, May 27, 2018 10:13 PM
    Sunday, May 27, 2018 6:15 AM
  • Taking Tim Roberts’ advice the lead in was changed to:

    #ifdef _WIN32
    #include "stdafx.h"
    #include <windows.h>
    #endif
    

    And VC complained about the unexpected #endif then added this:

    2 IntelliSense: PCH warning: header stop cannot be in a macro or #if block. An

    IntelliSense PCH file was not generated.

    d:\CODE_TESTS\STL_Comparisons\STL_Comparisons\STL_Comparisons.cpp

    28 1 STL_Comparisons

    I find that a bit ugly of Visual Studio.  Is it the case that when writing code for Linux and for Windows using VS that the programmer must comment out the line:  #include “stdafx.h” when compiling under Linux.

    Still, rather than using my own created defines I am switching to the existing defines.  Thank you Tim.



    Sunday, May 27, 2018 10:13 PM
  • If you don't mind the potential affect to compile time, why don't you just disable the precompiled headers in Visual Studio? It isn't like you need them.

    Precompiled headers were introduced as a way to speed up compile times due to large amounts of include files, but the way that they were done was quite simplistic. First you compile an empty source file with the precompiled header included, use this to save the parse tree for that header.

    Second, when the compiler includes the precompiled header, it will just dump the saved parse tree at the start of the module and use it as a starting point. Because the pch is used as a base, nothing can exist prior to it. This is why the compiler basically ignores everything before the include.

    Anyway, to disable the precompiled headers, delete stdafx.cpp and stdafx.h, then go to the project properties. If you set Precompiled Header to Not Using Precompiled Headers:

    then the compiler won't ignore everything before the include of stdafx.h. You should also delete the #include "stdafx.h" line too.

    If you don't want to want to remove the precompiled headers, then put the #ifdef inside the precompiled header file. This means that you will still have to include it on Linux systems.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Marked as answer by bkelly77 Monday, May 28, 2018 12:21 AM
    Sunday, May 27, 2018 11:18 PM
  • Darran,

    I disabled the pre-compiled as you suggested.  The project creation still insists on including stdafx.h but now it does not complain about the #ifdef and #endif surrounding it.

    Simple solution, great result.  Thank you for your time.

    Monday, May 28, 2018 12:21 AM