69

I recently moved from Java for C++ but now when I am writing my application I'm not interested in writing everything of the code in the main function I want in main function to call another function but this other function is in another .cpp file.

Let me explain better if you wouldn't understand:
I have one file: main.cpp inside it I have main function.

I have the second file: second.cpp inside I have a function called second() I want to call this function called second() from my main function..

Any help?

Blachshma
  • 17,097
  • 4
  • 58
  • 72
User
  • 1,458
  • 3
  • 16
  • 40
  • 8
    Are you learning C++ from [a good introductory book](http://stackoverflow.com/q/388242/46642)? – R. Martinho Fernandes Aug 09 '11 at 11:40
  • 1
    Well header files are the answer, so since you know about those it's not clear what you are doing wrong. Post the code you have and the error you get, otherwise we are just guessing. – john Aug 09 '11 at 11:41
  • I know this is an old question, but [learncpp.com recommends doing this by using forward declarations](https://www.learncpp.com/cpp-tutorial/programs-with-multiple-code-files/). Which technique is better? – Tyrcnex Feb 01 '23 at 12:01

4 Answers4

101

You must use a tool called a "header". In a header you declare the function that you want to use. Then you include it in both files. A header is a separate file included using the #include directive. Then you may call the other function.

other.h

void MyFunc();

main.cpp

#include "other.h"
int main() {
    MyFunc();
}

other.cpp

#include "other.h"
#include <iostream>
void MyFunc() {
    std::cout << "Ohai from another .cpp file!";
    std::cin.get();
}
Zandor Smith
  • 558
  • 6
  • 25
Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 3
    May I ask that the purpose of the .h file be further explained ? It seems to me that it contains no info unmentioned in the other.cpp file – James Well Sep 02 '17 at 10:02
  • 3
    I believe, it is not necessary to `#include "other.h"` in other.cpp – Blake Mar 31 '19 at 18:16
  • 5
    @MichaelD.Blake In this example, you are correct. However, real projects are often more complicated, and `other.h` would define _types_ that are used inside `other.cpp`. Usually `X.cpp` will include `X.h` so you may as well get used to it now. Other simplifications include lack of header guards in `other.h`. – Lightness Races in Orbit Mar 31 '19 at 18:51
  • @JamesWell `main.cpp` has no idea what's in `other.cpp`. All the "info" is what it sees in `other.h`! – Lightness Races in Orbit Mar 31 '19 at 18:52
  • @LightnessRacesinOrbit I don't understand why would someone want to `#include` a header file consists of forward declarations of this particular `.cpp` file into it back. what you're saying should be something like a header consists of forward declarations of functions, types and templates from multiple `.cpp` files including the one you're `#including`. I said it is not necessary because the one asking might think it is necessary to `#include` for it to work. – Blake Mar 31 '19 at 19:01
  • @MichaelD.Blake Again, you are correct in this particular case. But this particular case is rare. Usually the header will also include type definitions and other things that the source file requires to work _and_ which are required elsewhere. – Lightness Races in Orbit Mar 31 '19 at 21:23
  • 5
    A note to people who are receiving `undefined reference to myFunc()`; you need to include `other.cpp` in the compilation command, so the linker knows the implementation of `myFunc()`.... `g++ main.cpp other.cpp`. – jackw11111 Jun 21 '19 at 04:14
  • 1
    @jackw11111, yeah. There are issues with undefined reference. It means, in case of having multiple cpp files should I include all of them as arguments? – Hareen Laks Apr 15 '20 at 01:59
  • @MichaelD.Blake if you don't `#include "other.h"` in other.cpp you could get some really nasty bugs, other.cpp will have no idea what the signuture of `MyFunc` is supposed to be. If you define `int MyFunc() {return 0;};` in other.cpp without including the header the code will compile. – fejfo Sep 10 '21 at 10:42
25

You should have header files (.h) that contain the function's declaration, then a corresponding .cpp file that contains the definition. You then include the header file everywhere you need it. Note that the .cpp file that contains the definitions also needs to include (it's corresponding) header file.

// main.cpp
#include "second.h"
int main () {
    secondFunction();
}

// second.h
void secondFunction();

// second.cpp
#include "second.h"
void secondFunction() {
   // do stuff
}
Creat
  • 524
  • 1
  • 4
  • 9
8

In C/C++ you have header files (*.H). There you declare your functions/classes. So for example you will have to #include "second.h" to your main.cpp file.

In second.h you just declare like this void yourFunction(); In second.cpp you implement it like

void yourFunction() { 
   doSomethng(); 
}

Don't forget to #include "second.h" also in the beginning of second.cpp

Hope this helps:)

Ataur Rahman Munna
  • 3,887
  • 1
  • 23
  • 34
m_pGladiator
  • 8,462
  • 7
  • 43
  • 61
3

You can simply place a forward declaration of your second() function in your main.cpp above main(). If your second.cpp has more than one function and you want all of it in main(), put all the forward declarations of your functions in second.cpp into a header file and #include it in main.cpp.

Like this-

Second.h:

void second();
int third();
double fourth();

main.cpp:

#include <iostream>
#include "second.h"
int main()
{
    //.....
    return 0;
}

second.cpp:

void second()
{
    //...
}

int third()
{ 
    //...
    return foo;
}

double fourth()
{ 
    //...
    return f;
}

Note that: it is not necessary to #include "second.h" in second.cpp. All your compiler need is forward declarations and your linker will do the job of searching the definitions of those declarations in the other files.

Blake
  • 842
  • 6
  • 16