0

I am scratching my head at an error that I am having, I should be passing something simple. The situation is:

I have a h and a cpp file with a class implementation. This has been working well. I copy pasted these files to another project and I am trying to use them

But suddenly I got the

Scanning dependencies of target path_planning
[ 50%] Building CXX object CMakeFiles/path_planning.dir/src/main.cpp.o
[100%] Linking CXX executable path_planning
CMakeFiles/path_planning.dir/src/main.cpp.o: In function `main':
main.cpp:(.text+0x4df6): undefined reference to `PID::PID()'
main.cpp:(.text+0x53b4): undefined reference to `PID::~PID()'
main.cpp:(.text+0x5506): undefined reference to `PID::~PID()'
collect2: error: ld returned 1 exit status
CMakeFiles/path_planning.dir/build.make:94: recipe for target 'path_planning' failed
make[2]: *** [path_planning] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/path_planning.dir/all' failed
make[1]: *** [CMakeFiles/path_planning.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

I haven't changed anything from the project where it worked.

To reproduce the problem here is the code

    #ifndef PID_H
    #define PID_H
    
    class PID {
      public:
    
      PID();
    
      virtual ~PID();
    };
 #endif  // PID_H

and

#include "PID.h"

PID::PID() {}

PID::~PID() {}

in the main file I do

#include "PID.h"

and inside main()

PID pid;

I have tried it by itself (in a minimum example) and it works. So I cannot understand why it gives this error with my main file.

halfer
  • 19,824
  • 17
  • 99
  • 186
KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • 8
    We need to see your makefile. Looks like you are not compiling and linking to `PID.cpp` – NathanOliver Jan 18 '21 at 13:48
  • 2
    Why do you have this `#endif // PID_H` line in the cpp file and not in the header? – Eraklon Jan 18 '21 at 13:51
  • As an unrelated note, it is better to puth the `#endif` of the header guard in the bottom of the .h file instead of the .cpp file that includes it. – Tomer Jan 18 '21 at 13:51
  • Sorry about the #endif. It was a writing mistake. I corrected it already – KansaiRobot Jan 18 '21 at 13:55
  • @NathanOliver I think that is the reason. I edited the `CMakeLists.txt` to include the PID.h file and now it is linking. However, I thought this was done by `cmake ..` automatically, it does not? – KansaiRobot Jan 18 '21 at 13:57

0 Answers0