PlatformIO Community

Undefined Reference error : After separation of classes

I’m trying to separate Header file, C++ classes and main class.

When working in the same directory that is the src everything works perfectly fine(All classes in one place).
However, if I try to split classes(move Header files to include folder and c++ classes to **lib folder **. I am getting undefined reference.

I have read similar thread people were encouraged to add all the classes in src directory.

Does this issue has a solution or the best is to add all the files in one folder src

Below is the error that I’m getting

Linking .pio/build/uno/firmware.elf
/var/folders/tl/l4jm4xs90xdcpbx0_6vhfb3h0000gn/T//ccBKtLdi.ltrans0.ltrans.o: In function global constructors keyed to 65535_0_main.cpp.o.1804': <artificial>:(.text.startup+0x68): undefined reference toRobot::Robot(int, int, int)’
/var/folders/tl/l4jm4xs90xdcpbx0_6vhfb3h0000gn/T//ccBKtLdi.ltrans0.ltrans.o: In function main': <artificial>:(.text.startup+0xfc): undefined reference toRobot::normalTrafficFlow()’
collect2: error: ld returned 1 exit status
*** [.pio/build/uno/firmware.elf] Error 1

Yes, because you are violating the needed folder structure it seems? If you create a new “library” and put the header in your project’s include folder but the source code of the library in the library folder, this breaks their linkage.

Either create a new folder in lib in which you place both .h and .cpp files (if it’s supposed to be reusable code and has no direct dependencies on any of the code found in include and src) or put it in src and include.

Thanks, maxgerhardt… I have moved all the files(.h and .cpp files) to either lib directory or include directory and it’s perfectly working.

Is it possible to have all the Header files(.h) in include directory and all Source files(.cpp) in lib directory ?