Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module concept for C++ [closed]

Tags:

c++

c++11

C++ is still an evolving language and new features are being added to it over the years.

One feature that I miss badly in C++ is a proper module concept: the current approach using header files (where you use a conditional #define to make sure that the header is not included twice) seems definitely unsatisfactorily to me.

For example, in my project we have the problem that we have too many "#include"'s in many source files, making the compilation time unnecessarily long: it takes 45 minutes to build our product, using Incredibuild, i.e. using at least 10 cores in parallel. Therefore, we have to spend a lot of time cleaning up files manually, i.e. removing includes to check if they are really needed.

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

  1. separate clearly the interface from the implementation of a module;
  2. compile the interface and the body of a module separately (currently .h files are compiled again and again each time they are included in other files): a tool could then read the compiled interface and tell what types, functions, classes it exports;
  3. write tools for automatically rearranging imports more easily (e.g. with Java / Eclipse it is possible to rearrange all the imports of a file automatically).

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

EDIT

Thanks for the suggestion regarding precompiled headers. I will try it out if possible (We use Visual Studio 2008). Maybe we are using header files in the wrong way(?) We use one header file for each class. Then we have a cpp file with the class implementation. Often we end up with cpp files that include 30, 40 header files. When we change the cpp file, some includes are not needed any longer, but it is difficult to find out which ones. This is partly related to the fact that header files include other header files.

We spend too much time rearranging the imports and it seems there doesn't exist a tool that can do this automatically. It would save us a lot of time.

like image 879
Giorgio Avatar asked Sep 07 '11 22:09

Giorgio


People also ask

What is a module in C?

Modular programming consists of separating implementation from interface and hiding information in the implementation. In C this is achieved by placing the interface definition in a header file and the implementation in a source file. Disciplined use of static is used to hide implementation details.

What is modularity in C?

Modularity is closely tied with encapsulation; think of modularity as a way of mapping encapsulated abstractions into real, physical modules. The C/C++ convention is to create two files for each class: a header file (. h suffix) for the class interface, and an implementation file (.

What is modular programming example?

Examples of modular programming languages - All the object-oriented programming languages like C++, Java, etc., are modular programming languages.


1 Answers

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

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

One feature that I miss badly in C++ is a proper module concept: the current approach using header files (where you use a conditional #define to make sure that the header is not included twice) seems definitely unsatisfactorily to me.

Some compilers support #pragma once to avoid having to write the include guards, but it is non-standard as far as I know. There are situations where you don't want include guards; Boost.Preprocessor is an example of a library with some headers that intentionally don't have include guards.

For example, in my project we have the problem that we have too many "#include"'s in many source files, making the compilation time unnecessarily long: it takes 45 minutes to build our product, using Incredibuild, i.e. using at least 10 cores in parallel. Therefore, we have to spend a lot of time cleaning up files manually, i.e. removing includes to check if they are really needed.

Stroustrup has an FAQ entry on compile-time slowness. Also read up on GotW article #7 about including header files. It's highly likely that you're including way more files than necessary. For example, you may be able to get away with forward declarations. If you have huge header files, you may try splitting them up so that your sources include only the declarations you really need. It may be just a matter of your file structure not being conducive to fast compiles.

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

We have the PIMPL idiom (also known as a compilation firewall) for this. And even without it, I don't have any trouble putting the implementation in the .cpp file and the interface in the .h file (even though it's not a "pure" interface).

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

Some compilers support precompiled header files that you can take advantage of.

3.write tools for automatically rearranging imports more easily.

I don't understand what you mean by this.

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

Believe it or not, there is a proposal to add some kind of module concept to C++, but it hasn't been accepted into C++11 due to time constraints (they were working and reviewing other proposals like rvalue references, which in my opinion, is much more important). It might be included in the next version or update of the C++ standard.

like image 138
In silico Avatar answered Oct 06 '22 02:10

In silico