Search code examples
c++compilationheaderheader-filescompiler-optimization

Working with large header files


First a bit of background:

I am using Visual Studio 2017 - Professional Addition

Developing a native C++ android application

I used the standard template. So I have a pure c++ (11) library where all my code is. And a precompiled header defined in the main project that links to my library.

What I am doing

I am working on some code that requires a large amount of embedded data.

I googled around for good ways to do this, but most seem like way to much hassle and almost always there were comments saying just to put the data directly into header files as that's the most portable way.

So I now have a number of header files that basically contain static arrays of data. NOTE they are not 'const' as if you do that then Visual Studio 2017 will try and display the data if you happen to move over the variable definition. So just static.

All the header files are then aggregated into one header file and this is then finally referenced in a standard cpp file. effectively making the data private to that class.

This all works fine. BUT compile times are getting to be very slow. And if I watch the output window I can see that it spends 80% of its time on the cpp file (even when there have been no changes to the data or the code)

Now this cant be the best a compiler can do. I would expect the compiler to skip this as there have been no changes that directly effect the cpp file.

I have also attempted to move things into the precompiled header. But that simply makes the pch.h file take ages to build each time.

So what am I doing wrong?

Update

I have double checked the files have not been changed. If I press F6 (build) then press again, it will still rebuild the large files.

I have also tried reworking the static data so that it is defined in a cpp file. And this still makes no difference.

For clarity this is an example:

.cpp file:

#include "Some.h"

unsigned char _someData[] =
{
   0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00,
};

unsigned char* Some::GetSomeData(void)
{
   return _someData;
}

.h file:

#pragma once

class Some
{
public:
static unsigned char* GetSomeData(void);
};

Solution

  • In manner to save compilation time try the following:

    1. Make sure you your project doesn't contains missing files. It cause re-compile even when no changes at all).
    2. Don't put data in headers, place the data in c\cpp files and use the extern of the data variables in the headers.
    3. Do not include unecessary headers from other header. (any change in header will generate recompilation of the all)
    4. Always prefer to use forward declaration in headers when it is possible (instead of include).
    5. Notice the difference between #include <file> and #include "file". if you choose the wrong include method, (like use <> to include your file, or "" for system include) you spend more time in search for the file.
    6. Order the include dirs from the most common to the less common, it also may save time in search for file.

    If you wan't to know why a project or file is re-built or re-compiled even though there was no changes, you can do it as follows:

    1. Menu: tools -> options
    2. In the opened Options left pan: Project and solution-> Build and Run:
    3. Then in the right pan: change MSBuild project build output verbosity to Diagnostic.
    4. Build the project
    5. Find in the diagnostic data why it been re-compiled.

    Good luck