Referencing .h and .cpp class files

Ziegler
Posts: 11
Joined: Sun May 05, 2019 4:34 am

Referencing .h and .cpp class files

Postby Ziegler » Sun Jan 26, 2020 2:57 am

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.
Last edited by Ziegler on Sun Jan 26, 2020 5:00 pm, edited 1 time in total.

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: Referencing .h and .cpp class files

Postby ESP_Sprite » Sun Jan 26, 2020 3:53 pm

One thing to check is if exampleClass.cpp is actually compiled and linked in.

Ziegler
Posts: 11
Joined: Sun May 05, 2019 4:34 am

Re: Referencing .h and .cpp class files

Postby Ziegler » Sun Jan 26, 2020 4:59 pm

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.

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: Referencing .h and .cpp class files

Postby ESP_Sprite » Mon Jan 27, 2020 10:39 am

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.

Ziegler
Posts: 11
Joined: Sun May 05, 2019 4:34 am

Re: Referencing .h and .cpp class files

Postby Ziegler » Wed Mar 25, 2020 11:48 pm

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.

Who is online

Users browsing this forum: Baidu [Spider], Majestic-12 [Bot] and 128 guests