Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem w/ preprocessor

Problem w/ preprocessor

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpcsharpvisual-studioquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dean Goodman
    wrote on last edited by
    #1

    So I have a class that does not make use of MFC/Windows code in any way, though I sometimes use it in MFC projects. However, when I do use the class in an MFC project, VS.net complains that there is no '#include "stdafx.h"' line in the .cpp file of the class. Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) Today I tried to remedy this situation by adding an #ifdef at the beginning of the .cpp file as follows:

    #ifdef WINVER
    //wasn't sure about the best one to use
    //if you know of something better than WINVER, please let me know
    #include "stdafx.h"
    #endif

    ... other includes

    ... implementation of class

    However, if I add this code (at the very beginning of the file) VS.Net chokes with "fatal error C1020: unexpected #endif". Any ideas? --Dean

    J J D A 4 Replies Last reply
    0
    • D Dean Goodman

      So I have a class that does not make use of MFC/Windows code in any way, though I sometimes use it in MFC projects. However, when I do use the class in an MFC project, VS.net complains that there is no '#include "stdafx.h"' line in the .cpp file of the class. Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) Today I tried to remedy this situation by adding an #ifdef at the beginning of the .cpp file as follows:

      #ifdef WINVER
      //wasn't sure about the best one to use
      //if you know of something better than WINVER, please let me know
      #include "stdafx.h"
      #endif

      ... other includes

      ... implementation of class

      However, if I add this code (at the very beginning of the file) VS.Net chokes with "fatal error C1020: unexpected #endif". Any ideas? --Dean

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      Do not include atdafx.h in you file. Instead (1) goto projects->settings (2) select all configurations (3) select you file from tree view (4) select C/C++ tab (5) from drop down box select precompiled headers (6) check not using precompiled headers. Now when you compile while using MFC you will not gett the error about finding end of file while looking for precompiled header. Trust in the code Luke. Yea right!

      1 Reply Last reply
      0
      • D Dean Goodman

        So I have a class that does not make use of MFC/Windows code in any way, though I sometimes use it in MFC projects. However, when I do use the class in an MFC project, VS.net complains that there is no '#include "stdafx.h"' line in the .cpp file of the class. Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) Today I tried to remedy this situation by adding an #ifdef at the beginning of the .cpp file as follows:

        #ifdef WINVER
        //wasn't sure about the best one to use
        //if you know of something better than WINVER, please let me know
        #include "stdafx.h"
        #endif

        ... other includes

        ... implementation of class

        However, if I add this code (at the very beginning of the file) VS.Net chokes with "fatal error C1020: unexpected #endif". Any ideas? --Dean

        J Offline
        J Offline
        Joaquin M Lopez Munoz
        wrote on last edited by
        #3

        Probably your best option is not to include any #include <stdafx.h> and, when compiling into an MFC project, change the settings of the .cpp alone to "not using precompiled headers". Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        1 Reply Last reply
        0
        • D Dean Goodman

          So I have a class that does not make use of MFC/Windows code in any way, though I sometimes use it in MFC projects. However, when I do use the class in an MFC project, VS.net complains that there is no '#include "stdafx.h"' line in the .cpp file of the class. Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) Today I tried to remedy this situation by adding an #ifdef at the beginning of the .cpp file as follows:

          #ifdef WINVER
          //wasn't sure about the best one to use
          //if you know of something better than WINVER, please let me know
          #include "stdafx.h"
          #endif

          ... other includes

          ... implementation of class

          However, if I add this code (at the very beginning of the file) VS.Net chokes with "fatal error C1020: unexpected #endif". Any ideas? --Dean

          D Offline
          D Offline
          Dean Goodman
          wrote on last edited by
          #4

          Thanks to both of you... I had not even thought of that. I am still puzzled as to why I am getting this error, though. The #endif is not unexpected -- it matches the #ifdef and there is no other possible text beforehand to provide an unbalanced #if* #endif pair. --Dean

          J 1 Reply Last reply
          0
          • D Dean Goodman

            So I have a class that does not make use of MFC/Windows code in any way, though I sometimes use it in MFC projects. However, when I do use the class in an MFC project, VS.net complains that there is no '#include "stdafx.h"' line in the .cpp file of the class. Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) Today I tried to remedy this situation by adding an #ifdef at the beginning of the .cpp file as follows:

            #ifdef WINVER
            //wasn't sure about the best one to use
            //if you know of something better than WINVER, please let me know
            #include "stdafx.h"
            #endif

            ... other includes

            ... implementation of class

            However, if I add this code (at the very beginning of the file) VS.Net chokes with "fatal error C1020: unexpected #endif". Any ideas? --Dean

            A Offline
            A Offline
            Anders Molin
            wrote on last edited by
            #5

            Dean Goodman wrote: Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) That is so not true. It's about precompiled headers, and have nothing to do with MFC. If you make a new ATL project, in VC, you also get stdafx.h and .cpp I use precompiled headers in all my projects, even console applications because it speeds up the compile time. - Anders Money talks, but all mine ever says is "Goodbye!"

            D 1 Reply Last reply
            0
            • A Anders Molin

              Dean Goodman wrote: Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) That is so not true. It's about precompiled headers, and have nothing to do with MFC. If you make a new ATL project, in VC, you also get stdafx.h and .cpp I use precompiled headers in all my projects, even console applications because it speeds up the compile time. - Anders Money talks, but all mine ever says is "Goodbye!"

              D Offline
              D Offline
              Dean Goodman
              wrote on last edited by
              #6

              What I really meant was that when you create an MFC project (or ATL -- I wouldn't know since I never create this type of project), VS defaults to using a precompiled header. If you create a console application, precompiled headers are not used by default. I didn't mean to say that PCH's are specific to MFC. I just usually don't mess with that stuff -- I usually want to get my work done quickly ;). --Dean

              A 1 Reply Last reply
              0
              • D Dean Goodman

                What I really meant was that when you create an MFC project (or ATL -- I wouldn't know since I never create this type of project), VS defaults to using a precompiled header. If you create a console application, precompiled headers are not used by default. I didn't mean to say that PCH's are specific to MFC. I just usually don't mess with that stuff -- I usually want to get my work done quickly ;). --Dean

                A Offline
                A Offline
                Anders Molin
                wrote on last edited by
                #7

                Dean Goodman wrote: I didn't mean to say that PCH's are specific to MFC. Ok, sorry, then I misunderstood you :-O - Anders Money talks, but all mine ever says is "Goodbye!"

                B 1 Reply Last reply
                0
                • D Dean Goodman

                  Thanks to both of you... I had not even thought of that. I am still puzzled as to why I am getting this error, though. The #endif is not unexpected -- it matches the #ifdef and there is no other possible text beforehand to provide an unbalanced #if* #endif pair. --Dean

                  J Offline
                  J Offline
                  Joaquin M Lopez Munoz
                  wrote on last edited by
                  #8

                  The way PCHs are implemented in Visual C++, the compiler igoners everything up to the infamous #include <stdafx.h> line, which explains the unbalanced #endif. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                  1 Reply Last reply
                  0
                  • A Anders Molin

                    Dean Goodman wrote: I didn't mean to say that PCH's are specific to MFC. Ok, sorry, then I misunderstood you :-O - Anders Money talks, but all mine ever says is "Goodbye!"

                    B Offline
                    B Offline
                    basementman
                    wrote on last edited by
                    #9

                    Wouldn't it just be easier to create a .LIB containing the functions and link to it, rather than messing with the source code every time you use it? No hassle, just #include the .h file and set your linker libs line to include the .lib file. Also, one point of maintenance across all projects that use it.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups