-1

hope you guys are doing well. I am just getting linker error in C++ , I don't know why? Everything is correct.... Check below testing.h file


#ifndef __MYClass__
#define __MYClass__
#include<iostream>
using namespace std;
class Abc {
private:
    int a;
public:
    void input();
    void display();
};


#endif


and here's implementation of these functions in Functions.cpp file.


#include"testing.h"

void Abc::input() {
    cout<<"Enter any value : ";
    cin>>a;
}
void Abc::display() {
    cout<<"You Entered : "<<a;
}

And now, in main.cpp

#include<iostream>
#include"testing.h"
using namespace std;
int main() {
    Abc obj;
    obj.input();
    obj.display();
    return 0;
}

All files are compiled successfully. In main.cpp Linker says.... g++ -Wall -o "main" "main.cpp" (in directory: /home/Welcome/C++ Practices/testingLinux) /usr/bin/ld: /tmp/ccYI9LAy.o: in function main': main.cpp:(.text+0x10): undefined reference to Abc::input()' /usr/bin/ld: main.cpp:(.text+0x1c): undefined reference to `Abc::display()' collect2: error: ld returned 1 exit status Compilation failed. I'm using built-in linux compiler...

Abdullah0123
  • 3
  • 1
  • 2
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – ChrisMM Mar 26 '21 at 11:17
  • This doesn't address the question, but names that contain two consecutive underscores (`__MYClass__`) and names that begin with an underscore followed by a capital letter are reserved for use by the implementation. Don't use them in your code. – Pete Becker Mar 26 '21 at 13:39

2 Answers2

0

You are not compiling Functions.cpp file.
This should fix your issue:

g++ main.cpp Functions.cpp
Bharat S
  • 334
  • 1
  • 8
0

There are multiple ways you can fix this but before that please read up on Translation Unit.

Coming to your problem.

When you write

g++ -Wall -o main main.cpp

The compiler will pick up main.cpp for compilation and expand testing.h that includes the declaration for class ABC and with this header file it can determine what is the size of ABC and be able to generate instructions reserving space for obj on the stack. It can't see the definition for input() and display() hence defers that task to the linker. Note that testing.cpp is not in the picture at all since the compiler doesn't know that the implementation of ABC is in testing.cpp. Now when the linker tries to resolve the symbols input() it fails to find the definition for it and throws the error

undefined reference to Abc::input()

So, to fix this you can tell explicitly upfront that it also needs to take in testing.cpp while compiling main.cpp by

g++ -o main main.cpp testing.cpp

Another way is to create a dynamic library out of testing.h and testing.cpp

g++ -shared -fPIC testing.cpp -o libtest

and then link it against main.cpp

g++ -o main  main.cpp -I. -L. libtest

What this does is that the compiler still can't figure out the definition of input() and display() but the linker can since now the library containing the definitions is provided to it.

Zoso
  • 3,273
  • 1
  • 16
  • 27