0
// a.cpp
void foo() {
    uint16_t var_in_a;
}
// b.cpp
void foo() {
    uint16_t var_in_b;
}
// c.cpp
void foo() {
    uint16_t var_in_c;
}

In this example, I have to include <stdint.h> file in all .cpp files. I want to avoid this activity. I want to specify this <stdint.h> file in my builder / IDEs properties and make it include this file in every .cpp file.

Is there any way to make compiler to include specified files when compiling any .cpp files?

Please give an answer for following compilation tools / IDEs

  • visual studio code
  • visual studio
  • g++
arsdever
  • 1,111
  • 1
  • 11
  • 32
  • 1
    That's not the compiler's job. Some IDEs for some languages provide this functionality. C++ compilers don't. And for a good reason. – Ron May 09 '19 at 15:18
  • 1
    *Is there any way to make compiler to include specified files when compiling any .cpp files?*. Don't do that. It stops the code from being portable. Explicitly include all of the includes you need in the file to make it portable. – NathanOliver May 09 '19 at 15:19
  • 1
    You will have to be explicit somewhere. You could make one "bucket/util" header to pull I common things, but that might be asking for trouble, or look at pre-compile headers. Are you trying to save typing, or something else? – doctorlove May 09 '19 at 15:20
  • there could be several options from where to get a `uint16_t`, eventually it is you who has to ensure that you include the correct header – 463035818_is_not_an_ai May 09 '19 at 15:21
  • @doctorlove yes somehow. Mostly for not missing to include some files. Like this example. If I miss to include in one of `.cpp` files it will say `compile error`. But there's no problem to include the file in all files. – arsdever May 09 '19 at 15:22
  • @user463035818 yes, but I don't want to specify the include file in all source files. I want to specify it once. – arsdever May 09 '19 at 15:23
  • @arsdever you could prepare a single header that includes what you need and then include only that, but the best is still to include in every file exactly what you need in that file – 463035818_is_not_an_ai May 09 '19 at 15:24
  • Why do you only want to specify it once? – doctorlove May 09 '19 at 15:24
  • if you want to save typing then you better stop being lazy, otherwise I dont see why you would want that... – 463035818_is_not_an_ai May 09 '19 at 15:25
  • @doctorlove first - to specify exactly the same file for all sources, second - save some time on manually including in all files – arsdever May 09 '19 at 15:25
  • Are you looking for the g++ variant of `/FI`? – JVApen May 09 '19 at 15:47
  • @JVApen could you please forward some reference to that? – arsdever May 09 '19 at 15:50
  • 1
    For the visual studio compiler: https://learn.microsoft.com/en-us/cpp/build/reference/fi-name-forced-include-file?view=vs-2019 – JVApen May 09 '19 at 15:52
  • 1
    You might also be interested in https://stackoverflow.com/q/3387453/2466431 – JVApen May 09 '19 at 15:53
  • @JVApen exactly that is what I wanted. Thank you. – arsdever May 09 '19 at 15:54
  • Possible duplicate of [Include header files using command line option?](https://stackoverflow.com/questions/3387453/include-header-files-using-command-line-option) – JVApen May 09 '19 at 15:55
  • @JVApen I don't think this is a duplicate, because this question has a different header and isn't covered by the question you mentioned. – arsdever May 09 '19 at 16:01

2 Answers2

1

In short, no (not in general that works for every build system), you can't get compilers to include the header files they rely on. They have no way of knowing which ones might have the definitions or prototypes you use.

Now, you could make one header file that includes commonly used header files, but if some cpp file just needs a subset of these, the compiler is doing extra work and this potentially slowly down the build.

Some people put the common includes in a pre-compiled header.

You could argue that it's better to only include header files where they are needed, narrowing down when they get compiled/read.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
  • maybe there's some misunderstanding. I have edited the question. Please review it. I don't want the compiler to search for files had to include. – arsdever May 09 '19 at 15:32
  • It seems you want to not type `#include a_header.h` in any cpp file and tell the build system to include it, and the compiler to try to compile a cpp file and then go look up which header it may have needed if things go wrong, so form some big lookup table of all symbols to look up stuff in somewhere (a bit like pre-compiled headers). This is in danger of making things lots slower for the sake of saving some typing. – doctorlove May 09 '19 at 15:38
  • No. Actually, I want to specify a file to be included in every file, which's going to be compiled. – arsdever May 09 '19 at 15:42
  • 1
    I'm afraid the answer is still no. – doctorlove May 09 '19 at 15:47
  • 1
    please look at [@JVApen](https://stackoverflow.com/users/2466431/jvapen)s [comment](https://stackoverflow.com/questions/56062652/how-to-specify-files-to-included-before-compilation-by-default?noredirect=1#comment98766569_56062652) under the question – arsdever May 09 '19 at 15:55
  • There - a slight edit. See also here: https://stackoverflow.com/questions/32773283/cmake-include-header-into-every-source-file – doctorlove May 09 '19 at 16:14
1

As mentioned in comments, you are searching for a comma nd line include. See Include header files using command line option? for the details.

Visual studio has a similar compilation option: /FI. Official documentation: https://learn.microsoft.com/en-us/cpp/build/reference/fi-name-forced-include-file?view=vs-2019

JVApen
  • 11,008
  • 5
  • 31
  • 67