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

0

u/[deleted] Mar 11 '19

[deleted]

1

u/kalmoc Mar 11 '19

Every single header only library must effectively staisfies your requirements (of course we don't really need such a feature for using them, but it might still improve compile-times). And most modern non-header only libraries I've seen also do little more than saying "compile all those sources" for most of their code base. Againm use a build system iif you have to deal with corner cases or have more complex requirements, but most of the complexity I've seen in build files is 1) Around tests and or installation 2) Only compile file X if condition Y is satisfied, whre condition Y can most of the time just as well be detected with the preprocesspr (and often you do just that - gain in code anyway). If nothing else, every single code base I've seen has one or more variables called <some_name>_sources that holds a set of cpp files, which could be replaced by that technique and generally speaking: If this ever were standardized even more libraries would make sure that they can be compiled in that manner.

Again, I'm not saying it is a useful enough feature to go into the standard, but I think you are highly overdramatizing the situation.

0

u/[deleted] Mar 11 '19

[deleted]

1

u/kalmoc Mar 11 '19

Not sure what one thing has to do with the other, but:

The reason why everyone is excited about modules has nothing to do with simpler build systems (quite the contrary) or package management. All that I'm saying is that the suggested feature would be applicable to many more libraries than you are implying.

Now, depending on the company and sector you are working, in you might have to deal with a very different set of libraries in your day to day work, which might be the reason why our experiences are so different.