r/cpp • u/c-smile • 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.
5
u/kalmoc Mar 11 '19
It seems to me that people keep constantly assuming that adding some sourve files to your build script in language X is somehow difficult and hence needs to be further simplified. (E.g. via header only/ single file libraries or suggestions like this). Combining a bunch of c++ files into an application isn't difficult in any build system I'm aware of. Its not even difficult to do this with a blank compiler invocation (g++ a.c b.c d.c).
So why bother standardizing something like this?
5
u/Leandros99 yak shaver Mar 13 '19
It's called unity build. It's being done, but since you always recompile everything, the edit-compile-debug cycle is quite long. Though, I'm aware of a few libraries who do that to simplify compilation.
2
u/BCosbyDidNothinWrong Mar 11 '19
I'm on board that this is clever enough to be thought about. Having 'source' as the keyword is not a good idea though. #include unit, #unit, #compliation, #pragma unit, etc. etc. might be better ideas.
2
u/t_hunger Mar 11 '19
The preprocessor makes it very hard to do an IDE (and humans, too) since it changes the code in arbitrary ways before it is seen by the compiler.
So the direction is to remove the preprocessor, not to extend it's functionality.
1
u/kalmoc Mar 11 '19
Quite often a macro based technique is much more readable (both for IDE as well as for humans) than the equivalent TMP code. Sometimes I wonder if improving the preprocessor wouldn't have been a bet solution for some problems than banning it and trying to come up with "clever" ways to achieve the same in language.
In any case. I think that particular feature is useless and it wouldn't really be a preprocessor feature anyway.
5
u/AcademicImportance Mar 11 '19
what if you dont have the source code to those libraries? even if you do, now oyu compile each library in your executable and that's why we have shared libraries. to share them.
how do you do parallel compilation then? what if you have a 1 mil lines project and the resulting c/cpp file is gigabytes long and the compiler crashes because the AST is too big.
-17
0
Mar 11 '19
[deleted]
1
u/AcademicImportance Mar 11 '19
it still doesn't solve the "proprietary lirbaries" issue. nor the "extremely large project, that will get cobbled into on file resulting in an extremely large AST" issue.
1
u/SlightlyLessHairyApe Mar 11 '19
How does this work for symbols that are intended to exist in only one translation unit? It seems like anything defined anywhere (say, pnggamma has a gamma correct table) would then exist in every translation unit.
2
u/ShillingAintEZ Mar 11 '19
I don't think this is meant to include the source of the .cpp file, that could be done with #include.
Maybe #pragma depend would be better.
1
u/feverzsj Mar 11 '19
people are actually doing this using unity build, except you must take care name pollution.
1
u/kalmoc Mar 11 '19
Completely different story. What the OP suggests should still leave you with N individual translation units - just as if you involved the compiler with N source files.
0
Mar 11 '19
[deleted]
1
u/kalmoc Mar 11 '19
or you can only use it for dependencies that can be included as-is as if it was code from your own project (same language, same flags, no non-trivial configuration).
Considering that that is the most common case, I don't think that would be a problem. For everything else you can use a "real" build system. Personally I still don't think it is worth standardizing, but not due to limited functionality.
0
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>_sourcesthat 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
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.
1
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.cppthen upon parsing the list of input files will be appended as if you issued
g++ main.cpp a.cppAs 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.
4
u/yehezkelshb Mar 11 '19
If I understand you correctly, you are talking about including the object file of the "included" source in the final executable, i.e. giving it as an input to the linker, not the compiler. This is supported by MSVC with
#pragma comment(lib, "libname")And it doesn't remove the need for a proper build system, as other commenters mentioned already, as we need to handle dependencies, know when to invoke the compiler on a specific source file and when the previous compilation output can be reused etc.