0

I'am trying to compile a simple .cpp file using g++ on ubuntu.

Here are my three files:

main.cpp:

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

int main()

{

int a(2);

b = func2(a);     


return 0;

}

math.cpp:

#include "math.h"


int func2(int nombreRecu)

{

int valeur(nombreRecu + 2);


return valeur;

}

math.h:

#ifndef MATH_H_INCLUDED

#define MATH_H_INCLUDED


int func2(int nombreRecu);


#endif // MATH_H_INCLUDED

But when doing this command, g++ main.cpp, I get the error: Undefined reference to func2(int)

adrTuIPKJ44
  • 2,793
  • 2
  • 9
  • 8
  • 1
    hmm. `math.h` is a standard C header. Depending on your compiler and settings you might be including that header instead of your custom header. I would change the file names (like `my_math.h`) and compiler again. – NathanOliver Dec 14 '16 at 18:46
  • Same error when changing files name (from math.h and math.cpp to file.h and file.cpp for instance) – adrTuIPKJ44 Dec 14 '16 at 18:48
  • When asking questions about build errors, first of all make sure the code you show actually *have* the error you ask about. Then also include the actual error, complete, in full and unedited in the question body. – Some programmer dude Dec 14 '16 at 18:49
  • You forgot to include the `math.cpp` in the compile command. You should have something like `g++ main.cpp math.cpp` – NathanOliver Dec 14 '16 at 18:52

0 Answers0