0

I am quite new to C++, coming from an interpreted language I am having a bit of difficulty with understanding how header files relate to libraries and dependencies.

For example, suppose I have the main body of my project:

#include <vector>

//----------------------------define some auxiliary function which deals with vectors
void A(std::vector<int> &vect)
{ 
    vect.clear();
}

//----------------------------main body
int main()
{
    std::vector<int> vect = {1,2,3}
    A(vect);
    return 0 ;
}

Now suppose I want to define the auxiliary function A() in a separate .cpp file, and still be able to call it from my main .cpp file. My question is- for this project, do I have to #include <vector> in all .cpp files which use something from that library? If so, do I still need to #include <vector> in my main file?

I think I understand how header files work, but it is still unclear what happens if I have several files using elements from the same library.

2
  • 1
    Just the file that uses the vector needs to include it. Commented Sep 22, 2020 at 7:18
  • @MathewHD yes, but do all files which use it have to include it separately? Commented Sep 22, 2020 at 7:18

4 Answers 4

3

Just the file that uses the Vector Component needs to include it. You can also include a component when you include another file.

For Example:

  1. Example.h
#include <vector>

//----------------------------define some auxiliary function which deals with vectors
void A(std::vector<int> &vect) { 
    vect.clear();
}
  1. Example.cp
include "Example.h"

//----------------------------main body
int main() {
    std::vector<int> vect = {1,2,3};
    A(vect);
    return 0;
}

Comment from @Vivick

You would normally also #include <vector> in the main to avoid needing to look up which header file includes which library.

Sign up to request clarification or add additional context in comments.

5 Comments

Though it is good practice to also include vector in main to avoid looking up which header includes what
in your example main is using a vector, but does not include the header. If I keep the first line in main but replace Example.h by something else then main is broken. You should include what you use, not rely on includes by other includes
@Vivick It's not like OP can't do that. I just wanted to show that you don't have to include <vector> in every script but could use a include on the header file instead.
what can be done and what should be done are two different things. You should at least mention it
It has already been added as a comment, I can include the comment into the answer if wanted tough.
2

You don't have to include in all .cpp files. If you #include in your Example.h or Example.cpp and if you just include one of them in your main file like #include "Example.h" or "Example.cpp", you can use vectors in your main too.

Comments

2

Includes are transitive. #include foo.h simply puts the contents of foo.h in place of the include-directive.

This is correct, but bad:

// foo.h
#include <vector>
std::vector<int> v;

// bar.h 
#include "foo.h"

// main.cpp
#include "bar.h"   
std::vector<int> x;
int main() {}

This will compile, but it is very bad style. You should include what you use. If someone looks at foo.h only, suppose they realize that v is not needed anymore, they will see that no vector is used, remove #include <vector> and by this break main.cpp. You cannot look into all files that include foo.h to decide whether the include is needed or not.

This is correct and better:

// foo.h
#include <vector>
std::vector<int> v;

// bar.h 
#include "foo.h"

// main.cpp
#include "bar.h"   
#include <vector>
std::vector<int> x;
int main() {}

Some recommend to include standard library headers last. In this way you will get an error in case eg foo.h forgot to include <vector>. Sometimes headers need not the full definition of a type, but only a declaration. In such cases a forward declaration can be used to reduce compile times. The definition is then only supplied in the source by including the header. Example:

// foo.h
struct bar;   // forward declaration 
struct foo {
    bar* p;   // pointer, need only declaration, no definition!
    void do_something();
}

// foo.cpp
#include "bar.h"
void foo::do_something() {
    bar b;      // create instance, needs definition
    // ...
}

PS: You do not have to worry about including a header too often. Headers are guarded against multiple includes via so called include guards:

// moo.h
#ifndef MOO_HEADER_INCLUDE_GUARD
#define MOO_HEADER_INCLUDE_GUARD

// the actual contents

#endif

If in one translation unit the same header is included twice, the preprocessor (includes are handled by the preprocessor) will see that the symbol MOO_HEADER_INCLUDE_GUARD is already defined and skip the contents of the header.

Comments

1

Let's say you create a.h and a.cpp a.h

#pragma once //ensure a.h will only be included once in case multiple c++ include it
#include <vector>
void A(std::vector<int> &vect)

a.cpp

#include "a.h" // include your own declarations
void A(std::vector<int> &vect)
{
...
}

Whoever includes a.h will by a side effect also include <vector> but each module (.h) still needs to include all its own dependencies (ie if you need std::vector somewhere, you have to include it, otherwise your code might not compile any more if used in another context.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.