Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what about the order of global vs local include files in c++ [closed]

Tags:

c++

include

Is there a recommended practice that for example global includes shall go befor local includes. By global I mean #include <iostream> and local #include "myhdr.h". Is it some prefered order and why?

like image 325
lgwest Avatar asked Dec 15 '22 18:12

lgwest


1 Answers

Yes, there are recommendations. Some of them are:

  • Order: If you're in MyClass.cpp, put "MyClass.h" first, then C-headers, then STL headers, then your project's headers.
  • Internal order: In each of these categories, use alphabetical order.
  • Syntax: Use #inlcude <> for C and STL and #include "" for your own headers.

They should look something like this:

#include "MyClass.h"

#include <time.h>

#include <iostream>
#include <vector>

#include "MyFolder/MyAwesomeClass.h"
#include "MyOtherFolder/MyOtherClass.h"

For more recommendations on good coding style you can take a look at Google's C++ Style Guide. They give a good explanation on why you should do this in this section.

like image 99
alestanis Avatar answered May 10 '23 22:05

alestanis