10

I am trying to separate my functions in another source file. But i am getting error that multiple definition on add function.

Main source file

Main.cpp

#include<iostream>
#include "myHeader.h"
using namespace std;

int main()
{
int result = add(1,2);
}

Header file "myHeader.h"

#include "calc.cpp"
int add(int, int);

Other Source file "calc.cpp"

int add(int a, int b)
{
return a+b;
}
CODError
  • 779
  • 2
  • 10
  • 25

6 Answers6

14

What you need is:

"myHeader.h"

#ifndef MY_HEADER
#define MY_HEADER
 int add(int, int);
#endif 

calc.cpp

#include "myHeader.h"

int add(int a, int b)
{
 return a+b;
}

main.cpp

#include "myHeader.h"

int main()
{
  int result = add(1,2);
  return 0;
}

You don't include the .cpp into the .h . The header file is used to tell the compiler the existence of a function with the specified prototype, but the liker will be tke care of matching up the call to a function with the implementation of that function.

Also, it's usually a good idea to give you header file and .cpp the same name, so calc.h and calc.cpp rather than myHeader.h.

Sean
  • 60,939
  • 11
  • 97
  • 136
4

Don't include calc.cpp from myHeader.h. Except for that one line, your example is right as far as headers go. (main() should return a value).

calc.cpp and main.cpp are two different "compilation units" which will be compiled separately into object files. The two object files are then combined into one executable by a linker.

TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • Sorry for wrong systax. If i remove include statement from header file how is it that my main function finds the add function? – CODError Jan 16 '14 at 16:40
  • yes, thats working but how? My main function has the header file only. How is it finding the definition of add function? – CODError Jan 16 '14 at 16:41
  • 1
    @CodErr It finds the *signature* (the declaration) of the function in `calc.h` which you've included from your `main.cpp`. Having the signature is sufficient for the compiler to generate the object file. The actual body of the function goes into the object file for `calc.cpp`. The linker's job is to combine all the code together, so it's really the linker that "finds" the actual compiled code of your function. – TypeIA Jan 16 '14 at 16:42
  • Ok, let's say I save my calc.cpp at a location which is somewhere out of the project folder(anywhere in my comp HD). Then also linker can find it? I just want to understand where linker is searching for the source file which has the definition of functions. – CODError Jan 16 '14 at 17:28
  • @CodErr I'll refer you here for a lengthy discussion of what the compiler and linker do; understanding that should answer your question: http://stackoverflow.com/questions/9529571/c-compiler-and-linker-functionality – TypeIA Jan 16 '14 at 17:33
  • Thanks! I understand that we need the linker to know all the object files with all the methods defined. – CODError Jan 16 '14 at 17:47
1

You problem is that you include a Code File (cpp) into a header. You should do the inverse. Include your header "myHeader.h" into calc.cpp. And to be coherent, you should name your header the same name as your Code file, so calc.h for the header and calc.cpp for you code.

jordsti
  • 746
  • 8
  • 12
1

This is pretty simple. Do not Include your "calc.cpp" file in "MyHeader.h" file.

Take also a look at C/C++ IncludeGuard here

This is a fundamental of C/C++ programming. You will need to use it many times.

Protect your "myHeader.h"

#ifndef ADD_HEADER
#define ADD_HEADER
 int add(int, int);
#endif // ADD_HEADER
Axel Borja
  • 3,718
  • 7
  • 36
  • 50
0
#include "calc.cpp"

Don't do that - it includes the function definition in any translation unit that includes the header, so you'll end up with multiple definitions.

Without that, your code should be fine if you build a program from both source files. main.cpp will include a declaration, so that it knows that function exists; the definition from the other source file will be included in the program by the linker.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

Do not include calc.cpp . This is causing the redefinition

you can include myHeader.h in calc.cpp

vathsa
  • 303
  • 1
  • 7