C++ Do I have to include standard libraries for every source file?

↘锁芯ラ 提交于 2020-02-01 02:53:48

问题


I'm a bit confused at the moment because I'm planning to include multiple source and header files for the first time in one of my projects.
So I'm wondering if this would be the right approach?
Do I have to include the string header in every source file that uses it directly?
And what about the "stdafx.hpp" header that Visual C++ wants me to include?

Would that be the way to go?

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;
}

回答1:


  1. A good practice is usually to include only what your code uses in every file. That reduces dependencies on other headers and, on large projects, reduce compilation times (and also helps finding out what depends on what)

  2. Use include guards in your header files

  3. Don't import everything by polluting the global namespace, e.g.

    using namespace std;
    

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

  4. You don't need stdafx.h in your project unless you're using precompiled headers. You can control this behavior in the VS project properties (C/C++ -> Precompiled Headers -> Precompiled Header)




回答2:


The stdafx.h header is needed if precompiled header is enabled in VS. (Read this one) You only need to include the stdafx.h in your .cpp files as the first include.

Regarding the header and cpp files (which come in pairs), include things necessary for the declaration in the header, and include everything else (necessary for the definition) in the cpp. Also include the corresponding header in its cpp pair too. And use include 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 include myclass.h and call generate() function.




回答3:


The stdafx include should be at the top of every .cpp file and it should NOT be in .h files. You could put #include < string > in stdafx.h if you don't want to put it in every other file.




回答4:


I suppose that you must be having your own header files also which might be requiring in other cpp files and header files. Like the one you gave

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

In my opinion, its better to create one common header file in which you include all the common library header files and your project header file. This file then you can include in all the other cpp files and header file. And it will be better to use header guards also.

So, considering a common header file "includes.h".

#ifndef INCLUDES_H
#define INCLUDES_H

#include <string>

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

/***Header files***/    

#endif  //INCLUDES_H

This is now your common header file. This you can include in all your project files.



来源:https://stackoverflow.com/questions/25870961/c-do-i-have-to-include-standard-libraries-for-every-source-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!