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

1

u/[deleted] Mar 13 '19

[removed] — view removed comment

1

u/c-smile Mar 13 '19

How would you do this?

Consider this main.cpp file:

#define MAIN     
#include source "a.cpp"
void main() {...}

If you will run this :

g++ main.cpp 

then upon parsing the list of input files will be appended as if you issued

g++ main.cpp a.cpp

As of #defines and other environment variables: a.cpp inherits ones that are active at the point of "inclusion". So a.cpp will see MAIN defined.

Nothing too sophisticated I would say.