74

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?

4
  • 8
    Are you learning C++ from a good introductory book? Commented Aug 9, 2011 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. Commented Aug 9, 2011 at 11:41
  • I know this is an old question, but learncpp.com recommends doing this by using forward declarations. Which technique is better? Commented Feb 1, 2023 at 12:01
  • @Tyrcnex This is basically what the accepted answer talks about and its standard way of splitting things into multiple files in C++. Which way is better is mainly how you prefer to structure your project. Commented Mar 12, 2024 at 8:03

4 Answers 4

107

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();
}
Sign up to request clarification or add additional context in comments.

9 Comments

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
I believe, it is not necessary to #include "other.h" in other.cpp
@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.
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, yeah. There are issues with undefined reference. It means, in case of having multiple cpp files should I include all of them as arguments?
|
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
}

Comments

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:)

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.