1

When attempting to compile an externally provided C++ codebase, I've encountered a confusing problem:

Several of the header files are missing #include <MyLibrary.h>, where MyLibrary is an obvious dependency that is simply missing, and preventing compilation.

My question is: When compiling C++ code, is there a way to automatically include a dependency header file, without needing to #include it in each of the .h/.cpp files where it is required? In other words, a way to provide the C++ preprocessor a list of header files to automatically include when compiling all of the source code?

I understand that this is probably a very bad idea, but I'm trying to determine if the code I've been provided is simply broken, or if there's some way it could be compiled without manually fixing each file with missing dependencies.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • Not that I know of. Suggestion: are you sure you are compiling in the right standard, and that you have those libraries installed? – asu Nov 02 '16 at 21:18
  • 1
    1) C is not C++ is not C. 2) Your question is not clear. Headers are **source** code. They are not used in any way at run-time. And `#include` is the standard way to use external definitions. What other way are you thinking about? – too honest for this site Nov 02 '16 at 21:18
  • 1
    In Visual Studio the is definitely a way to do this - it will include a file(s) in all .cpp file in a project. It's one of the preprocessor options. – Anon Mail Nov 02 '16 at 21:20
  • Be there a way or not. Don't go that path! You probably break more than you fix. What's the problem adding the dependencies the way they are meant to be? – too honest for this site Nov 02 '16 at 21:21
  • @Olaf Obviously that you have to touch many source files which you may not want, for example for integrity, because you don't have write permission on them, because there are too many of them for just experimenting... I understand that the OP asks this not as a best practice for SW engineering but for a quick test. – Peter - Reinstate Monica Nov 02 '16 at 21:33
  • @Olaf In this particular situation, I'm attempting to compile a large, vendor-provided codebase. I need to code to compile, but I would like to make as few changes as possible. I agree that using the -include option can lead to obfuscated code, and should be avoided when possible. It's important to know that it exists, though, in order to understand why certain compilation errors may occur. – Some Java Programmer Nov 02 '16 at 21:36
  • @PeterA.Schneider: Wrong question: why are the dependencies missing in the first place? The question is about a work-around for code which is broken by design. – too honest for this site Nov 02 '16 at 21:40
  • @SomeJavaProgrammer: Kick the vendor where it hurts for providing broken-by-design code. Twice if you pay for it. If they already fail at such basics, I'd suspect there are much more serious problems with that code. (I'm was not always that relentless, but had to learn it the hard way) – too honest for this site Nov 02 '16 at 21:41
  • My answer has accumulated 3 downvotes. Does anyone know what they're downvoting for? With just the 1 original downvote, which came immediately, I was sure it was a social downvote, designed to prevent upvotes. It's common. But 3 downvotes sounds less like purely social, and more like some common misconception out there; after all they're downvoting simple facts. Does anyone know (or suspect) what it is? – Cheers and hth. - Alf Nov 02 '16 at 22:43
  • @Cheersandhth.-Alf I wondered about that -- your answer is a superset of mine (and none of the additional information is wrong, afaics). It should get *more* net votes, not fewer. Maybe more than one person doesn't like you. Don't waste time with idiots. – Peter - Reinstate Monica Nov 03 '16 at 01:33

2 Answers2

3

The C++ language standard does not support "global headers", but individual compilers do.

It's called a forced include.

With Visual C++ it's the option /FI, and with g++ it's option -include.


With Visual C++, however, the usual way to include common headers is to place those includes in a file called stdafx.h, and include that file in every translation unit, first of all. That's part of Visual C++'s precompiled header support. It's problematic because when it's turned on (and it's on by default in a Visual Studio project) it changes the preprocessing rules so that some standard code may not compile, but it can speed up larger builds considerably.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
2

Yes, at least with gcc and compatibles.: Use the -include option. See https://stackoverflow.com/a/3387518/3150802.

Community
  • 1
  • 1
Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
  • That's a really bad idea and defies build-tools. – too honest for this site Nov 02 '16 at 21:20
  • @Olaf I have never used it, but I cannot see immediately the validity of either claim. Can you elaborate? – Peter - Reinstate Monica Nov 02 '16 at 21:21
  • How would e.g. Scons detect the additional dependencies? Let alone if you change dependencies, you have to edit an unexpected file (the build file) instead of the one which is actually is affected (the source code). Much the opposite of the important "least surprise" design principle. – too honest for this site Nov 02 '16 at 21:24
  • @Olaf Ah, I see. Yes, dependency analyzers will likely ignore it (unless you use gcc proper for it...) But then, it should be trivial to edit this dependency into SCons since the config files are Python... – Peter - Reinstate Monica Nov 02 '16 at 21:31
  • Not sure if gcc will respect incoudes from the command line. Re manually editing: That's exactly my second issue with that. However you see it: it is a very bad idea and bad design practice. – too honest for this site Nov 02 '16 at 21:38