Multiple function definitions

Hi everyone,

I have a cpp file with my main function. At some point it calls for functions which I have declared in another cpp file. Example:

main.cpp
1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include"functions.cpp"

using namespace std;

int main(){
    cout << "Hello world" << endl;
    function_call();
    return 0;
}


functions.cpp
1
2
3
4
5
6
7
8
#include<iostream>

using namespace std;

void function_call(){
    cout << "Bye bye world" << endl;
    return;
}


As I try to compile the project, I get this error message:
multiple definition of 'function_call()'


If I copy the function_call function and paste it over my main function, and delete the functions.cpp file, it works fine. Can someone tell me whats wrong?
Sources are compiled; headers included -- with guards.

See http://www.cplusplus.com/articles/Gw6AC542/

(That has mainly classes in other files, but same goes for functions: declaration in header, implementation in source.)
I don't see where you're including functions.h in functions.cpp, also make sure to use include guards.

Can you post your functions.h and your build command line?
Last edited on
The problem is line 2 of your main.cpp file.

You're including your functions.cpp file here. The compiler is also picking up and compiling the functions.cpp file, resulting in two copies of function_call() being compiled. Line 2 should include functions.h, not functions.cpp.
keskiverto, thanks for the source material.

tipaye, I do not have a functions.h file, what should I put in it?

AbstractionAnon, so, are you suggesting that the solution is to change the name of functions.cpp to functions.h, and that's it?

Thanks for your answers.
http://www.cplusplus.com/forum/general/140198/

> I do not have a functions.h file, what should I put in it?
function declarations void function_call();

no need for headers guards if you are not defining anything (class/struct, inline or template functions), but they don't hurt you.
Thank you to everyone, I learned something new today.
Topic archived. No new replies allowed.