2
I've created three files namely test.cpp, function.h and function.cpp in a folder. test.cpp contains this code : 

#include <bits/stdc++.h>
#include "function.h"

using namespace std;

int main()
{
    freopen ("input.txt", "r", stdin);
    freopen ("output.txt", "w", stdout);

    int a,b;
    cin >> a >> b;

    cout << add(a,b) << endl;

    return 0;
}

function.h contains this code:

#include <cstdio>

using namespace std;

int add(int a,int b);

function.cpp contains this code:

#include <cstdio>

using namespace std;

int add(int a,int b);

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

This produces some error which says reference in add(int,int) is undefined. Here is the error message:

/tmp/ccbx3TEZ.o: In function main': test.cpp:(.text+0x81): undefined reference toadd(int, int)' collect2: error: ld returned 1 exit status [Finished in 2.0s with exit code 1]

Please, suggest me what to do.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
Protik Nag
  • 511
  • 5
  • 20
  • be sure you link the function.cpp. Besides, int add(int a,int b); the preceded declaration should only be in header, not also in source. – Soner from The Ottoman Empire Mar 21 '19 at 08:58
  • Additionaly, function.cpp does not include function.h – Tobias Brösamle Mar 21 '19 at 09:01
  • 3
    In addition to the duplicate which will explain what exactly such linking errors mean and how to fix them, please also read [why you should not `#include `](https://stackoverflow.com/q/31816095/1782465) and why [`using namespace std;` is a bad idea](https://stackoverflow.com/q/1452721/1782465) – Angew is no longer proud of SO Mar 21 '19 at 09:01
  • You should show us the exact command you use to compile - and crucially, link - those sources. It looks a lot like you've simply missed `function.o` from your link command. – Toby Speight Mar 25 '19 at 18:12

0 Answers0