Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
}
like image 943
Forivin Avatar asked Sep 16 '14 14:09

Forivin


People also ask

Do you need to include libraries that are in header file?

If your class declaration references types in the header, you will need to include it there. If it's only in the implementation, then you can include it in the cpp file.

Why is C++ Standard Library required?

The C++ Standard Library provides several generic containers, functions to use and manipulate these containers, function objects, generic strings and streams (including interactive and file I/O), support for some language features, and functions for everyday tasks such as finding the square root of a number.

Where should all elements of the standard C++ library be declared?

Each element of the C++ standard library is declared or defined (as appropriate) in a header .

Is Std a library in C++?

The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms, and iterators. It is a generalized library and so, its components are parameterized.


2 Answers

  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)

like image 165
Marco A. Avatar answered Sep 24 '22 08:09

Marco A.


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.

like image 37
Gyebro Avatar answered Sep 21 '22 08:09

Gyebro