Page 1 of 1

Referencing .h and .cpp class files

Posted: Sun Jan 26, 2020 2:57 am
by Ziegler
I'm having trouble referencing class methods from the related .cpp file and hoping for some idea as to why it's failing.

I've created an exampleClass with a .h file that includes:
  1. extern "C" {
  2.     void app_main(void);
  3. }
  4.  
  5. class exampleClass {
  6.     public:
  7.         void print();
  8.  
  9.     private:
  10. };
and I've created the associated exampleClass.cpp file that has:
  1. #include "exampleClass.h"
  2.  
  3. void exampleClass::print() {
  4.    
  5. }

However, in my main when after I create an object of the class, I call the class method and get the error:

(.literal.app_main+0x20): undefined reference to `exampleClass::print()'
main/libmain.a(main.cpp.obj): In function `app_main':
C:...\Project\build/../main/custom_accData.cpp:53: undefined reference to `exampleClass::print()'

Does anyone know why this is failing? Thanks for any help.

Re: Referencing .h and .cpp class files

Posted: Sun Jan 26, 2020 3:53 pm
by ESP_Sprite
One thing to check is if exampleClass.cpp is actually compiled and linked in.

Re: Referencing .h and .cpp class files

Posted: Sun Jan 26, 2020 4:59 pm
by Ziegler
ESP_Sprite wrote:
Sun Jan 26, 2020 3:53 pm
One thing to check is if exampleClass.cpp is actually compiled and linked in.
I'm not quite sure what you mean. Isn't the compilation and linking performed during the idf.py build/flash process? I'm not aware of any manual process that needs to be performed when creating a class.

Re: Referencing .h and .cpp class files

Posted: Mon Jan 27, 2020 10:39 am
by ESP_Sprite
Well, depending on where the file is and if you use CMake or the old make process, you may have to tell the build system your cpp file actually exists and needs to be compiled as well. If you failed to do that, you would get exactly this error.

Re: Referencing .h and .cpp class files

Posted: Wed Mar 25, 2020 11:48 pm
by Ziegler
Thanks ESP_Sprite. I'd put the project away and was only recently able to return. I was able to do this with help from your comments. I ended up going to the CMakeLists.txt file and added the file names as below:

set(COMPONENT_SRCS "example1.cpp"
"example2.cpp"
"main.cpp")
set(COMPONENT_ADD_INCLUDEDIRS ".")

register_component()

Thanks for posting. Cheers.