Module concept for C ++

C ++ is still an evolving language, and new features have been added to it over the years.

One of the features that I missed poorly in C ++ is the correct module concept: the current approach using header files (where you use the conditional #define to make sure the header is not included twice) seems unsatisfactory.

For example, in my project, there is a problem that we have too many “#include” in many source files, which makes the compilation time too long: it takes 45 minutes to create our product using Incredibuild, i.e. using at least 10 cores in parallel. Therefore, we must spend a lot of time manually cleaning the files, i.e. Removal involves checking if they are really necessary.

I think it would be very useful to have a module concept that allows

  • clearly separate the interface from the implementation of the module;
  • compile the interface and the module body separately (currently .h files are compiled again and again each time they are included in other files): the tool can then read the compiled interface and indicate what types, functions, classes it exports;
  • Writing tools to automatically rearrange imports is easier (for example, using Java / Eclipse, you can automatically rearrange all file imports).

Do you think that it is possible to define such a concept of the module and integrate it into C ++ or will it be too complicated? Do you know of any efforts in this direction?

EDIT

Thanks for the suggestion regarding precompiled headers. I will try if possible (we are using Visual Studio 2008). Maybe we use the header files wrong (?) We use one header file for each class. Then we have a cpp file with a class implementation. Often we get cpp files containing 30, 40 header files. When we change the cpp file, some of them are no longer needed, but it is difficult to determine which ones. This is partly due to the fact that header files include other header files.

We spend too much time reordering imports, and there seems to be no tool that can do this automatically. This will save us a lot of time.

+7
source share
2 answers

C ++ is still an evolving language, and new features are added to this as part of the development of C ++ 0x.

The C ++ 11 standard has already been approved and published , so there are no more functions added to it. Not for at least a few more years.

One of the features that I missed badly in C ++ is the correct module concept: the current approach using header files (where you use the conditional #define to make sure the header is not included twice) seems definitely unsatisfactory to me.

Some compilers support #pragma once to avoid the need to write included guards, but as far as I know, it is non-standard. There are situations when you do not want to include guards; Boost.Preprocessor is an example library with some headers that intentionally do not contain defenders.

For example, in my project there is a problem that we have too many “#include” in many source files, which makes the compilation time unreasonably long: it takes 45 minutes to create our product using Incredibuild, i.e. Using at least 10 cores in parallel. Therefore, we will have to spend a lot of time cleaning the files manually, i.e. removal includes to check if they are really necessary.

Stroustrup has a record of frequently asked questions about slow compilation . Also read the GotW # 7 article on including header files. It is very likely that you have included more files than necessary. For example, you can get away with advanced ads. If you have huge header files, you can try to split them so that your sources include only those ads that you really need. Perhaps this is due to the fact that your file structure does not contribute to fast compilation.

1. clearly separate the interface from the implementation of the module;

We have the PIMPL idiom (also known as compilation firewall). And even without this, I have no problems with the implementation in the .cpp file and the interface in the .h file (although this is not a "clean" interface).

2. connect the interface and the module body separately (currently .h files are compiled again and again each time they are included in other files): the tool can then read the compiled interface and indicate what types, functions, classes it exports;

Some compilers support precompiled header files that you can use.

3.write tools to automatically rearrange imports easier.

I do not understand what you mean by that.

Do you think that you can define such a concept for a module and integrate it into C ++ or will it be too complicated? Do you know what efforts are in this direction?

Believe it or not, there is a suggestion to add some kind of module concept in C ++, but it was not accepted in C ++ 11 because of (they worked and considered other sentences, such as rvalue links, which in my opinion , much more important). It may be included in the next version or update of the C ++ standard.

+10
source

This is not how C ++ is designed to work. Perhaps you could do this if you poked and pushed the linker enough to read the index file rather than redraw the headers. However, your compiler should only compile the modified files or have dependencies that have changed anyway. And, besides, the headers are not actually “compiled” into meaningful code (if they do not have implementations, which in some cases is correct). This is just a note to the compiler that this token will be found later on linking.

Short answer: No, you should not try to implement a home-brew 'module system. I really doubt that header files are what happens forever unless you use them incorrectly.

Edit: As someone noted, I neglected header files, which may have something to compile, which usually happens in large libraries or something that uses a lot of templates. Thus, I was mistaken in a "really dubious file header that takes forever."

0
source

All Articles