C ++ Should I include standard libraries for each source file?

I am a bit confused at the moment, because I plan to include several source and header files for the first time in one of my projects.
So I wonder if this will be the right approach?
Should I include a line header in every source file that uses it directly?
What about the stdafx.hpp header that Visual C ++ wants to include?

Will it be the way?

main.cpp

#include "stdafx.hpp" #include <string> //? #include <stringLib1.h> #include <stringLib2.h> using std::string; //use a windows.h function here //use a stringLib1 function here //use a stringLib2 function here 

stringLib1.h

 #include "stdafx.hpp" #include <string> using std::string; class uselessClass1 { public: string GetStringBack1(string myString); }; 

stringLib1.cpp

 #include "stdafx.hpp" string uselessClass1::GetStringBack1(string myString) { return myString; } 

stringLib2.h

 #include "stdafx.hpp" #include <string> using std::string; class uselessClass2 { public: string GetStringBack2(string myString); }; 

stringLib2.cpp

 #include "stdafx.hpp" string uselessClass2::GetStringBack2(string myString) { return myString; } 
+5
source share
4 answers
  • Good practice usually only includes what your code uses in each file. This reduces dependencies on other headers and, in large projects, reduces compilation time (and also helps figure out what depends on what)

  • Use include guards in header files

  • Do not import all pollution into the global namespace, for example

     using namespace std; 

    but rather qualify what you intend to use when you need it

  • You do not need stdafx.h in your project unless you use precompiled headers . You can control this behavior in the properties of the VS project (C / C ++ → Precompiled Headers → Precompiled Header)

+3
source

The stdafx.h header stdafx.h required if the allowed header is included in VS. ( Read this option ) You only need to include stdafx.h in your .cpp files as the first.

As for the header and cpp files (which fall in pairs), include the things needed for the declaration in the header, and include everything else (needed for the definition) in cpp. Also include the appropriate header in your cpp pair. And use enable guards .

myclass.h

 #ifndef MYCLASS_H // This is the include guard macro #define MYCLASS_H #include <string> using namespace std; class MyClass { private: string myString; public: MyClass(string s) {myString = s;} string getString(void) {return myString;} void generate(); } 

myclass.cpp

 #include <stdafx.h> // VS: Precompiled Header // Include the header pair #include "myclass.h" // With this one <string> gets included too // Other stuff used internally #include <vector> #include <iostream> void MyClass::generate() { vector<string> myRandomStrings; ... cout << "Done\n"; } #endif 

Then in main(...) you can just turn on myclass.h and call the generate() function.

+3
source

stdafx include must be at the top of every .cpp file, and it must NOT be in .h files. You can put #include <string> in stdafx.h if you do not want to put it in every other file.

0
source

I believe that you should also have your own header files, which may be required in other cpp files and header files. Like the one you gave

 #include <stringLib1.h> #include <stringLib2.h> 

In my opinion, it is better to create one common header file, which includes all the common library header files and the project header file. This file can then be included in all other cpp files and the header file. And it will be better to use header protectors.

So, given the general header file "includes.h".

 #ifndef INCLUDES_H #define INCLUDES_H #include <string> #include <stringLib1.h> #include <stringLib2.h> /***Header files***/ #endif //INCLUDES_H 

Now this is your common header file. This can be included in all of your project files.

-1
source

Source: https://habr.com/ru/post/1202680/


All Articles