Calling source files

I'm wondering how to call a source file (or resource or header)...
In case I have "main.cpp" and "minor.cpp", how should I call "minor.cpp" when I'm in "main.cpp"?
You need a header with the declarations of the symbols defined in minor.cpp,
#include it in both source files,
pass the two source files to the compiler/linker

http://www.cplusplus.com/forum/articles/10627/
I still don't get how to call the actual file, because the header (*.h) is filled with functions, so if you call a function it gets it from the header, but I'm still not able to use the second source-file where I can output some things and get user-input nor return from the second file back to the main file...
Please answer this question too =)

PS I read the link you sent, it was very useful but unfortunately didn't answer all my questions =(
The .cpp contains the definitions, and the .h(pp) contains the declarations.

If you want to call the functions, just include the corresponding .h file (minor.h) in the source file you want to call it from (main.cpp) and then call it like normal.

Then when compiling you simply need to tell the compiler to link the two .cpp files. If you are using an IDE it will probably link them for you as long as you have them in the correct directories of your project.
You don't call files. You call functions. The bodies of those functions can be in any .cpp file you want.

Example:

1
2
3
// b.h

void foo();

1
2
3
4
5
6
7
8
9
10
11
12
13
// a.cpp

#include <iostream>
#include "b.h"

int main()
{
  std::cout << "We are now in a.cpp\n";

  foo(); // call the foo function (in b.cpp)

  return 0;
}

1
2
3
4
5
6
7
8
9
// b.cpp

#include <iostream>
#include "b.h"

void foo()
{
  std::cout << "we are now in b.cpp\n";
}
Last edited on
So I only have to say there is a function in the header, and define it in the source file?
Thanks for help =)
Topic archived. No new replies allowed.