includes usage ?

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
binbinhfr
Posts: 78
Joined: May 9th, 2019, 10:57 pm

includes usage ?

Post by binbinhfr » May 22nd, 2020, 7:06 am

Hi guys,

as my code is getting bigger, I would like to know if there is a common usage concerning includes ?

Especially: is it ok to put system includes in my own .h, or should I clear my .h from any include and put includes only in .cpp ?

Because now, I have system includes a little bit everywhere, and it's not easy to track this...
or even to make a little bit of cleaning to lighten the compile time
(because now some of these includes became useless because I do not use the functions anymore).

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: includes usage ?

Post by albinopapa » May 22nd, 2020, 7:23 am

What do you mean by "system includes"?

As far as including headers in source files, this only prevents polluting namespaces really. It CAN help with compile times depending on how many source files include that header when something changes it in.

Something I've done is comment out all the headers and uncomment one by one to see which ones are necessary for that header file. If a header file shows no errors, but you get compiler/linker errors in the cpp files, move what's left to the cpp files.

Keep the scope of what's included to where it's actually needed. If you have a header that is only used in a cpp somewhere, then just include it in that cpp file. I do this for the STL header files as well. If I am going to use <cmath> I'm probably going to only use it in a function defined in the cpp file, so that's where I'll include it.

I use to do forward declarations, but they are a pain in the ass getting the scoping correct as well as some times forgetting if something is a class or a struct or accidentally misspelling one of the declarations.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: includes usage ?

Post by albinopapa » May 22nd, 2020, 7:25 am

I guess what I'm trying to say is C++ is all about scopes. Keep things in the smallest scope possible.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

binbinhfr
Posts: 78
Joined: May 9th, 2019, 10:57 pm

Re: includes usage ?

Post by binbinhfr » May 22nd, 2020, 1:19 pm

albinopapa wrote:
May 22nd, 2020, 7:23 am
What do you mean by "system includes"?
in general include to sources that are external to your program : in general in the form <header.h>, but can be boost, sfml, or anything external, that you do not control.
albinopapa wrote:
May 22nd, 2020, 7:23 am
Something I've done is comment out all the headers and uncomment one by one to see which ones are necessary for that header file. If a header file shows no errors, but you get compiler/linker errors in the cpp files, move what's left to the cpp files.
Tedious job... ;-)
albinopapa wrote:
May 22nd, 2020, 7:23 am
Keep the scope of what's included to where it's actually needed. If you have a header that is only used in a cpp somewhere, then just include it in that cpp file. I do this for the STL header files as well. If I am going to use <cmath> I'm probably going to only use it in a function defined in the cpp file, so that's where I'll include it.
Yes, I know this, but when the code is getting bigger in size and time (I mean that you wrote some parts long ago, forgetting why you included <cmath>),
than it's hard to remember that you included <cmath> for a function that is perhaps not used anymore. So it leads to your previous remark about this tedious job.

I just wonder if there was a tool to do this job automaticaly : it would say things like "warning : stupid programmer, why do you still include this header, you do not use any of its functions !!!" :-D

googling "c++ find superfluous includes" gives some returns, but I'm not good enough in english to digest all this stuff and find a nice tools taht I could use under VC++ 2019.
albinopapa wrote:
May 22nd, 2020, 7:25 am
I guess what I'm trying to say is C++ is all about scopes. Keep things in the smallest scope possible.
That's precisely what I want to do.

Now the code should be around 100.000 lines and with my bad memory, it is hard to handle...

I revamp all the multithreading stuff, because I was obviously doing it the bad way (my CPU was 100% as soon as I have too many bots). Now I use mutex and condition variables to syn all this correctly (I guess), and the CPU is around 15%, even with 100 bots running their programs in parallel. See new pictures enclosed :

https://imgur.com/a/1IvTHgB
and
https://imgur.com/a/wgaHKJe

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: includes usage ?

Post by albinopapa » May 22nd, 2020, 6:50 pm

I just did a shallow google search and it seems a lot have been written for gcc compilers, but very little for visual studio. The only real suggestion was to enable 'showincludes' from within the project properties. All this does though is list a tree of includes your project has in the Output window during compilation. I just did this to see how useful it would be and even in my small project, there are quite a few STL and it's associated implementation headers listed so it does make it difficult to track your files.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Slidy
Posts: 80
Joined: September 9th, 2017, 1:19 pm

Re: includes usage ?

Post by Slidy » May 23rd, 2020, 4:05 am

ReSharper extension has that feature I believe

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: includes usage ?

Post by albinopapa » May 23rd, 2020, 5:57 am

ReSharper C++ anyway, the original ReSharper was for C# and .Net.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply