0

Hello (or good evening perhaps), Being a beginner in C++, I decided for the first time to split my code into several parts. So I created a main.cpp file, a carre.hpp file and a carre.cpp file with the following codes :

main.cpp

#include <iostream>
#include <string>
#include "carre.hpp"

using namespace std;

void direBonjour(){
   cout << "Bienvenue dans le Metteur au Carre !!!" << endl;
}

int main(){
   direBonjour();
   double a(0.), b(0.);
   cout << "Entrer le nombre que vous voulez mettre au carre :" << endl;
   cin >> a;
   b = con(a);
   cout << "Nombre Originale : " << a << ". Nombre au Carre : " << b << "." << endl;
}

carre.cpp

#include "carre.hpp"

double con(double x){
   double result(x*x);
   return result;
}

carre.hpp

#ifndef CARRE_HPP
#define CARRE_HPP

double con(double x);

#endif // CARRE_HPP

When I run my program in the terminal I get this error:

undefined reference to `con(double)'collect2.exe: error: ld returned 1 exit status

I use Visual Studio Code and I use MinGW, the C/C++ extension integrated to VS Code as well as Code Runner to run my programs. I created these files one by one by putting them in the same folder. I think it's a compilation problem but I haven't found solutions to my problem.

I hope you can help me.

Thank you in advance.

Axel Hippolite.

  • 4
    What are you using to compile? I'm guessing you aren't compiling carre.cpp. Plenty of references around, but this one should help: https://stackoverflow.com/questions/44212087/undefined-reference-when-including-a-header-file – Tas Mar 16 '20 at 01:05
  • I agree with you, I use Code Runner and the C/C++ extension in VS Code (with MinGW) and I've already looked for a way to change the compilation parameters but I couldn't find anything that would solve my problem. Do you have an idea? – Axel Hippolite Mar 16 '20 at 01:25
  • 1
    Somewhere, you told it to build `main.cpp`. Add `carre.cpp` to that list. – Carl Norum Mar 16 '20 at 03:38

0 Answers0