Problem with compilation

Hi
i wanna execute this program with gcc, but i had this problem:

undefined reference to `ajouteDeux(int)'
collect2: ld returned 1 exit status


math.h:

#ifndef MATH_H_INCLUDED
#define MATH_H_INCLUDED


int ajouteDeux(int nombreRecu);

#endif // MATH_H_INCLUDED

math.cc:
#include "math.h"

int ajouteDeux(int nombreRecu)
{
int valeur(nombreRecu + 2);

return valeur;
}

main.cc:

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

int main(void)
{
int a(2),b(2);
cout << "Valeur de a : " << a << endl;
cout << "Valeur de b : " << b << endl;
b = ajouteDeux(a); //Appel de la fonction
cout << "Valeur de a : " << a << endl;
cout << "Valeur de b : " << b << endl;

return 0;
}


Did any one know what is wrong??
add #include "math.cc" after #include "math.h"
or rename your "math.cc" file into "math.cpp".
I change "math.cc" inti "math.cpp" but it doesn't work.
Why i shoud add #include "math.cc", normally it work only with #include "math.h"!!!!!!
Because you didn't add math.cpp to your project probably, depending on your IDE. Now try to #include "math.cpp" anyways.
@Bouya - I hope you are doing this to compile both source files:
g++ main.cc mathe.cc
instead of compiling just main.cc.

I think you are doing g++ main.cc
Then since you do not compile the definition for your function it is going to report a linker error as it does.

Last edited on
what about only


#include "math.h"

int ajouteDeux(int nombreRecu)
{
int valeur(nombreRecu + 2);

return valeur;
}

main.cc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "math.h"
using namespace std;

int main(void)
{
int a(2),b(2);
cout << "Valeur de a : " << a << endl;
cout << "Valeur de b : " << b << endl;
b = ajouteDeux(a); //Appel de la fonction
cout << "Valeur de a : " << a << endl;
cout << "Valeur de b : " << b << endl;

return 0;
}
@bluecoder - That is also a solution , but it 1) changes the original code structure/organization which the original question posted(Not that it is a big project or something, but more of a test study code) 2) Putting function definitions in header file, although ok, but not a good thing. Because if u change the function in the header file, every source file which includes this header, needs to be recompiled after that, which could be time consuming and unncessary if that source includes the header not for the function but for some other data(structure , class ) from this header file.
Topic archived. No new replies allowed.