r/cpp • • Mar 11 '19

#include source "file.cpp", why not?

What if together with #include "file.cpp" the language would have something like #include source "file.cpp" ?

#include source "file.cpp"

Includes the file as a separate compilation unit rather than just inserting it in place as in case of existing #include.

Having such #include we may include libraries as simple as this:

 #include source "png.c"

 // where png.c file is just a list of all files from libpng:
 #include source "pngerror.c"   
 #include source "pngpriv.c"   
 #include source "pnggamma.c"   
 ...

This simple feature may eliminate the need of make, cmake, and pretty much all others from the zoo of makes. #include source and exiting C/C++ preprocessor should be enough in 90% of make cases.

Yet I think #include source can be used as a modules alternative.

Yet it is backward compatible - pretty much any exiting C/C++ library can be included that way in final executable without modification of library sources.

2 Upvotes

28 comments sorted by

View all comments

Show parent comments

1

u/yehezkelshb Mar 11 '19

So you still need the #include of the corresponding header file(s) to get the declarations of entities defined in b.cpp and c.cpp and used by a.cpp file?

1

u/c-smile Mar 11 '19

So you still need the #include of the corresponding header file(s)

Yes, #include "header" works and required as it is, nothing changes in this respect.

  • a.hpp defines external API of "a" module.

  • a.cpp defines its implementation.

Thus if your module consist of just these two files then its consumer will need both:

#include "a.hpp"
#include source "a.cpp"

And that is conceptually the same thing as import a;

The only major difference is that #include source is backward compatible - any existing C/C++ library can be modularized this way without touching existing sources.

1

u/yehezkelshb Mar 11 '19

What happens if I have 2 files in my project and both do #include source for the same a.cpp?

1

u/c-smile Mar 11 '19

Options are:

  • To react in the same way as currently with this: g++ a.cpp a.cpp ( same file appears in linker list twice)

  • or to treat content of #include source as having #pragma once now.

Otherwise you can always define a.module file with this content:

#pragma once
#include "a.hpp"
#include source "a.cpp"

And use it as

#include "a.module"

1

u/yehezkelshb Mar 11 '19

To react in the same way as currently with this: g++ a.cpp a.cpp ( same file appears in linker list twice)

Linkage error. OK. But no one would write such a command manually (and no build system would generate such a command), but it's easy to to so now with the #include source addition. (And let's don't start with the nested dependencies.) So the chances are you upset the users. They'll have to keep track on which file they put the #include source to make sure it appears exactly once.

or to treat content of #include source as having #pragma once now.

#pragam once works because it affects only the current invocation of the compiler. Do you suggest an external "DB", where the compiler has to keep track of which source has been compiled already and going to the linker input and thus shouldn't be compiled and linked again? What this means about compiling more than one source file in parallel?

Oh, and we haven't touch the point that most of the real projects never use g++ to generate the executable directly from the source. Each source file is separately compiled into an object file and only then the object files are linked together to create the final executable. How this works with the suggested #include source, or this is intended only for single-file projects?

1

u/c-smile Mar 11 '19

These are technical details, not a show stopper, right?

You don't need DB, file system is pretty adequate for that.

MSVC supports this (ugly I shall admit) construct already:

 #pragma comment(lib, "some.lib") 

to include static libraries into final executable. And that #pragma comment(lib, "some.lib") may appear multiple times. So all that is doable in principle.

→ More replies (0)