Saturday, February 10, 2018

dllmain example

tips and tricks on pre-compiled header

tips and tricks on pre-compiled header. my note is taken from the following posts.

  1. Precompiled headers
  2. Preparations

    With Visual C++ 6.0 you have to run devenv.exe with the undocumented /y3 option.
    In Visual Studio .Net you can get similar functionality by going to Tools->Options->Projects->VC++ Build and setting Build Timing to Yes.

    add some variation on this line to all copies of windows.h on my machine:
    #pragma message("Compiling windows.h - this should happen just once per project.\n")

    Setting up precompiled headers

    The only precompiled header settings that are efficient to use are the Create/Use pair of settings.( The documentation for these settings tells you what each setting does, but it fails to give you the big picture - it doesn't tell you how to setup a project to use these two together.)

    The basic idea is that one cpp file is specified as creating the precompiled header file and the other cpp files uses that precompiled header file. Because only one source file ever creates the precompiled header file it is guaranteed that it will only get built once per total rebuild.

    First you need to have one header file that every source file in your project will include. This is typically called stdafx.h or precompiled.h. If you don't have such a file, create it now. Check it to make sure it is including the appropriate set of header files - big header files that never change. Now make sure that every source file in your project is including this header file as the first non-comment thing that they do.

    Now you need one source file whose only job is to create the precompiled header file. It's best to have a source file exclusively for this purpose because every time this source file is modified, the .pch file will be regenerated. Generating the .pch file is an expensive operation that we are trying to avoid. This source file will typically be called stdafx.cpp or precompiled.cpp - to match the header file.

    Now it's time to go to project settings.

    1. First, make sure you select "All Configurations" so that your fixes will affect both debug and release builds of your project.

    2. Now, go to the C/C++ tab, precompiled headers category. Select "Use precompiled header file" and type in the name of your precompiled header file (stdafx.h or precompiled.h).

    3. If you try to build now your build will fail, giving a cryptic error about how it doesn't know how to create the .pch file. You need to tell it how.
      In the explorer view in project settings, open up your project and navigate down to the .cpp file that you chose for creating the precompiled header file (in VS.Net you will select that file from the Solution Explorer).
      Select that file, with "All Configurations" still selected, and select "Create precompiled header file", and type in the name of your precompiled header file.

    4. It kind of makes sense once you go through the steps. You give VC++ one source file to create the precompiled header file, and the other files all use it. This guarantees that it won't be rebuilt unnecessarily.
      You have to #include this file from all of your source files so that if you turn precompiled headers off, your program will compile in the same manner.

    5. The one fly in the ointment, the worrisome aspect of this feature, is that there is actually no guarantee that your program will behave identically, because VC++ doesn't enforce that your #include of the precompiled header file comes first. It enforces that you include the precompiled header file in each source file, but it will happily ignore includes, defines, and other code before that include. If you notice yourself getting some strange errors after changing your precompiled header files, that's probably why.
      Go through all of your source files and make sure that your precompiled header file is included first.

    6. That's it. If you have any problems then double check all of the steps, check the problematic source files for any preprocessor directives or code before the include of the precompiled header file, and maybe do a rebuild-all for good measure. If you want an example of a properly setup project to use as a reference, use the AppWizard to create an MFC project.

    7. You may be surprised. With Visual Studio .Net you can get a hierarchical list of header files used by setting Properties->C/C++->Advanced->Show Includes to yes.

    The defaults are dangerous

    For some projects - typically non-MFC projects, but the specifics depend on what version of VC++ you are using - the project wizard's set the precompiled header option to the seductive sounding "Automatic use of precompiled headers." Microsoft itself has admitted that this option is inefficient - yet they make it the default for many projects. Unless you are exquisitely careful this "automatic" use of precompiled headers translates to "rebuilding the precompiled headers for every source file." It is difficult to avoid this and keep it working properly. Don't use it. Either disable precompiled headers, or use them properly.

    If you use the DirectX AppWizard to create a non-MFC project, the newly created project will use the "Automatic use of precompiled headers" setting. Because of this these projects will take approximately twice as long to build as they need to. You need to manually fix them to get good build performance.

  3. Speed up C++ compilation, part 1: precompiled headers

Friday, February 9, 2018

set up MASM in visual studio

lcc compiler

PE utility

graphics API research