Friday, December 18, 2020

pre-compiled header file configuration

pre-compiled header file configuration.

I tried many times and finally I got it! how to set up precompiled header file and output file is straight:

  1. StdAfx.h
  2. a good tutorial on how to use stdafx.h file

  3. fatal error C1010: unexpected end of file while looking for precompiled header.
  4. fatal error C1010 - “stdafx.h” in Visual Studio how can this be corrected?
  5. Precompiled Header Files from Microsoft
  6. When you create a new project in Visual Studio, a precompiled header file named pch.h is added to the project. (In Visual Studio 2017 and earlier, the file was called stdafx.h.) The purpose of the file is to speed up the build process. Any stable header files, for example Standard Library headers such as , should be included here. The precompiled header is compiled only when it, or any files it includes, are modified. If you only make changes in your project source code, the build will skip compilation for the precompiled header.

    The compiler options for precompiled headers are /Y. In the project property pages, the options are located under Configuration Properties > C/C++ > Precompiled Headers. You can choose to not use precompiled headers, and you can specify the header file name and the name and path of the output file.

    Already Reviewed and Understood

  7. “unexpected end of file while looking for precompiled header. Did you forget to add ‘#include “stdafx.” to your source?”
  8. How to implement precompiled headers into your project
  9. this is the answer I finally got it:

    1. You stdafx.cpp should include stdafx.h and be built using /Yc"stdafx.h".
    2. Your other *.cpp should be include stdafx.h and be built using /Yu"stdafx.h".
    3. Note the double-quote characters used in the compiler options!
    4. on project level: in Visual Studio 2017 Solution View, select the project node and right click it. from Configurations Proery-> C/C++ -> precompiled headers-> Precompiled Header: set "Use/ Yu"; Precompiled Header File: set "stdafx.h"; precompiled header output file: I need to play around it:)
    5. on source code stdafx.cpp file level:in Visual Studio 2017 Solution View, select "stdafx.cpp" file and right click it. from Configurations Proery-> C/C++ -> precompiled headers-> Precompiled Header: set "Create/ Yc"; Precompiled Header File: set "stdafx.h"; precompiled header output file: in debug mode, set to : ".\$(Configuration)\TestString.pch". in release mode, set to: ".\$(Configuration)\TestString.pch". the file name for .pch , you can specify any name you like. here is what I discovered. :). or to be easy, in "All Configurations" selected, set it to: ".\$(Configuration)\TestString.pch".
    6. on any .cpp source code files other than "stdafx.cpp" file level:in Visual Studio 2017 Solution View, select "stdafx.cpp" file and right click it. from Configurations Proery-> C/C++ -> precompiled headers-> Precompiled Header: set "Use/ Yu"; Precompiled Header File: set "stdafx.h"; precompiled header output file: I need to play around it:)
    Here's a screenshot of the Visual Studio settings for stdafx.cpp to create a precompiled header:

    another good summary is given below:

    "stdafx" is just a convention. It's in no way mandatory. In a multi-project solution, I've used other setups with multiple pre-compiled headers for different parts. E.g. it may be useful to have one PCH shared by your UI projects, and another one for your database projects.

    The relevant components are the X.h file listing precompilable headers, the X.cpp file that includes only X.h (and adds no code itself), and the X.pch file created by compiling X.cpp (and thus X.h) with compiler option /Yc.

    When you're now compiling Y.cpp file with /Yu"X.pch", the compiler reads and discards everything up to #include "X.h". At that point it replaces its internal state with the state stored in X.pch, except for the input stream (remains Y.cpp, with the file pointer set to the next line after #include "X.h").

  10. C Language Dynamic String
  11. I used this as example to configure precompiled header file.

  12. What is stdafx.cpp file that is created automatically in a new c++ project in visual studio 2012
  13. Pre-compiled headers can greatly speed the up the compilation of the .cpp files in your project. By convention, it is stdafx.h that #includes all of the .h files that you want to be precompiled. You can name it anything you want but the project template just picks stdafx.h

    But before that can work, there must be one .cpp file that #includes stdafx.h and gets compiled first. Before all the other .cpp files in your project. Once that's done, the compiler can use the .pch file that was created and quickly compile all the other .cpp files, no longer having to actually #include the headers anymore.

    Stdafx.cpp is that one .cpp file. It has a setting that's different from all the other .cpp files, it gets built with /Yc. The "Create precompiled header file" option.

    UPDATE: after 28 years, Microsoft broken their "there used to be an std framework but nobody ever saw it" practices at VS2019. Now named "pch" instead of "stdafx", surely for the more obvious acronym. Just a name change, the mechanics are still the same.

  14. What is stdafx.cpp file that is created automatically in a new c++ project in visual studio 2012
  15. a good exercise to practice my understanding

  16. fatal error C1010 - “stdafx.h” in Visual Studio how can this be corrected?
  17. Turn off pre compiled headers:

    Project Properties -> C++ -> Precompiled Headers -> set Precompiled Header to "Not Using Precompiled Header".

  18. What is “stdafx.h” used for in Visual Studio?
  19. All C++ compilers have one serious performance problem to deal with. Compiling C++ code is a long, slow process.

    Compiling headers included on top of C++ files is a very long, slow process. Compiling the huge header structures that form part of Windows API and other large API libraries is a very, very long, slow process. To have to do it over, and over, and over for every single Cpp source file is a death knell.

    This is not unique to Windows but an old problem faced by all compilers that have to compile against a large API like Windows.

    The Microsoft compiler can ameliorate this problem with a simple trick called precompiled headers. The trick is pretty slick: although every CPP file can potentially and legally give a sligthly different meaning to the chain of header files included on top of each Cpp file (by things like having different macros #define'd in advance of the includes, or by including the headers in different order), that is most often not the case. Most of the time, we have dozens or hundreds of included files, but they all are intended to have the same meaning for all the Cpp files being compiled in your application.

    The compiler can make huge time savings if it doesn't have to start to compile every Cpp file plus its dozens of includes literally from scratch every time.

    The trick consists of designating a special header file as the starting point of all compilation chains, the so called 'precompiled header' file, which is commonly a file named stdafx.h simply for historical reasons.

    Simply list all your big huge headers for your APIs in your stdafx.h file, in the appropriate order, and then start each of your CPP files at the very top with an #include "stdafx.h", before any meaningful content (just about the only thing allowed before is comments).

    Under those conditions, instead of starting from scratch, the compiler starts compiling from the already saved results of compiling everything in stdafx.h.

    I don't believe that this trick is unique to Microsoft compilers, nor do I think it was an original development.

    For Microsoft compilers, the setting that controls the use of precompiled headers is controlled by a command line argument to the compiler: /Yu "stdafx.h". As you can imagine, the use of the stdafx.h file name is simply a convention; you can change the name if you so wish.

    In Visual Studio 2010, this setting is controlled from the GUI via Right-clicking on a CPP Project, selecting 'Properties' and navigating to "Configuration Properties\C/C++\Precompiled Headers". For other versions of Visual Studio, the location in the GUI will be different.

    Note that if you disable precompiled headers (or run your project through a tool that doesn't support them), it doesn't make your program illegal; it simply means that your tool will compile everything from scratch every time.

    If you are creating a library with no Windows dependencies, you can easily comment out or remove #includes from the stdafx.h file. There is no need to remove the file per se, but clearly you may do so as well, by disabling the precompile header setting above.

    Good example to use precompiled header file

  20. Projects in Visual C++ 2010 – Part 1: Creating a DLL project
  21. Projects in Visual C++ 2010 – Part 2: Project Dependencies
  22. Projects in Visual C++ 2010 – Part 3: Precompiled Headers
  23. How to Optimize Compilation Times with Precompiled Headers (PCH Files)
  24. Misc Applications

  25. Tooltips for Toolbars in Non-CFrameWnd Windows
  26. Single View in MultiDoc-Application
  27. Documenting Header Files
  28. 0.8 — A few common C++ problems

No comments:

Post a Comment