Calling a C++ class-method

User avatar
dasarne
Posts: 8
Joined: Mon May 06, 2019 8:09 pm

Calling a C++ class-method

Postby dasarne » Sat Nov 02, 2019 7:52 pm

Hello, everybody,
I want to call a method in a C++ class from an event in ESP-IDF in an xTask.
Here is the class and method „dispatch“ I want to call:
  1. /*
  2.  * Display.h
  3.  *
  4.  *  Created on: 01.11.2019
  5.  *      Author: arne
  6.  */
  7.  
  8. #ifndef COMPONENTS_HARDWARE_DISPLAY_H_
  9. #define COMPONENTS_HARDWARE_DISPLAY_H_
  10.  
  11. #include "Modell.h"
  12.  
  13. class Display {
  14. public:
  15.     Display();
  16.     virtual ~Display();
  17.     /**Analysiert die Befehle des Frontends und ruft entsprechende Methoden auf*/
  18.     void dispatch(char *str);
  19. private:
  20.     Modell theModell;
  21. };
  22.  
  23. #endif
  1. /*
  2.  * Display.cpp
  3.  *
  4.  *  Created on: 01.11.2019
  5.  *      Author: arne
  6.  */
  7.  
  8. #include <Display.h>
  9.  
  10. Display::Display() {
  11.     // TODO Auto-generated constructor stub
  12.  
  13. }
  14.  
  15. Display::~Display() {
  16.     // TODO Auto-generated destructor stub
  17. }
  18.  
  19. void Display::dispatch(char *str) {
  20.  
  21. }
To encapsulate the method in an external "C" method I wrote the following wrapper:
  1. /*
  2.  * DisplayC-Wrapper.h
  3.  *
  4.  *  Created on: 01.11.2019
  5.  *      Author: arne
  6.  */
  7.  
  8. #ifndef COMPONENTS_HARDWARE_DISPLAYC_WRAPPER_H_
  9. #define COMPONENTS_HARDWARE_DISPLAYC_WRAPPER_H_
  10.  
  11. #ifdef __cpluscplus
  12. class Display;
  13. extern "C" {
  14. #else
  15. struct Display;
  16. typedef struct Display Display;
  17. #endif
  18. Display *Display_new();
  19.  
  20. void Display_dispatch(Display *instance, char *str);
  21.  
  22. #ifdef __cpluscplus
  23. }
  24. #endif
  25. #endif
Here is the implementation of the wrapper:
  1. /*
  2.  * DisplayC-Wrapper.c
  3.  *
  4.  *  Created on: 01.11.2019
  5.  *      Author: arne
  6.  */
  7.  
  8. #include "DisplayC-Wrapper.h"
  9. #include "Display.h"
  10.  
  11. Display *Display_new()
  12. {
  13.     return new Display();
  14. }
  15.  
  16. void Display_dispatch(Display *instance, char *str)
  17. {
  18.     instance->dispatch(str);
  19. }
And here the corresponding call in the xTask:
  1. void rx_task() {
  2.     Display *instance = Display_new();
  3.     Display_dispatch(instance,"Test");
  4. }
Everything compiles but doesn't link.
I got the following error-message at linkage time:

What do I have to do to make this linking?
Are there suitable compiler flags?
Is it possible to address C++ classes from C methods at all?
Is there more information about this topic somewhere? The best would be to find a working example...

Many greetings
Arne
All theory, my friend, is grey, But green is life's glad golden tree.

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: Calling a C++ class-method

Postby PeterR » Sun Nov 03, 2019 11:26 am

Passing the class instance around as 'void *' and then casting is the usual trick.
If the class member dispatch is static then you can just pass the member address as a function pointer.

Code: Select all

extern "C" CToCPlusPlus(void *displayPtr, const chat *str) {
   ((Display *)displayPtr)->dispatch(str);
}
& I also believe that IDF CAN should be fixed.

Who is online

Users browsing this forum: Google [Bot] and 73 guests