Issue with calling C++ static class member function

Hi,

I have a C++ class with code as in below:

//.h file
class Model_Server
{
    public:
           static void Command_Handler( );
};

//.cpp file
void Model_Server::Command_Handler(  )
{
    printk( "Received command. \n ");
}

This I need to call in a .c file, which I did with extern "C" definitions with a C wrapper as in below:

//.c file
#ifdef __cplusplus
extern "C" {
#endif 
void Wrapper()
{
	Model_Server::Command_Handler( );
}
#ifdef __cplusplus
}
#endif

I am getting errors if I compile this, same above static class member function I am able to call in a .cpp file and build successfully.

But I am unable to call in .c file. 

The errors I am getting when calling from .c file are:
1. In class declaration : 


2. In .c wrapper function where I am calling static class member function I am getting the error "
 at the scope resolution operator  

Model_Server::Command_Handler( );


Kindly help me solve this.
P.S: CMakeLists & prj.conf are in order and I have enabled C++ support, auto-complete feature is also working I am able to go to static class member function definition by clicking on the function call & like I said same call builds fine in .cpp file.
Thanks

 

  • Hello ,

    it is possible to use static function which forwards to the class method by using static class pointer. However, this would require assigning the pointer so that it is known which class instance should be used when static pointer is called

    Thanks, I tried this out, but I am unable to call static function inside the model_handler.c file.
    To call a static function, I need to write a wrapper C++ file & Wrapper API, this wrapper API i can call in model_handler.c.

    For example:
    1. If I have C++ static API as in below in a file model_server.cpp:

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    
    static void Model_Server::Command_Handler()
    {
    	int a;
    }
    
    #ifdef __cplusplus
    }
    #endif
    

    2. I need to write a wrapper.cpp for a wrapper API as in below, by marking C++ includes with extern "C":

    extern "C"{
    #include "model_handler.h"
    }
    
    void wrapper_file_API()
    {
        Model_Server::Command_Handler();
    }

    3. Then finally I can call the first API into .c file as in below with wrapper:

    static void wrap()
    {
    	wrapper_file_API();
    }

    As you can see my code increases if I add wrappers like this,

    If I call the C++ class member static function straightaway in .c file as in below:

    void Wrap()
     {
      Model_Server::Command_Handler();
     }

    GCC doesn't recognize the static member function syntax "Model_Server::Command_Handler();" and gives me build errors.


    So, can you please suggest how I can get to call the static member function and assign the handler pointers without writing wrappers...?

    I am struggling with the syntax

    Thanks,

  • Hi,

    I think you would need some wrappers in your code. You could try something like this

    //class_header.h file
    class Model_Server
    {
        public:
               static void Command_Handler( );
    };
    
    
    //wrapper.h file
    #ifdef __cplusplus
    extern "C" {
    #endif 
    void wrapper();
    #ifdef __cplusplus
    }
    #endif
    
    
    //.cpp file
    #include "class_header.h"
    #include "wrapper.h"
    
    void Model_Server::Command_Handler( );
    {
        // some print for testing goes here
    }
    
    extern "C" void wrapper()
    {
        Model_Server::Command_Handler();
    }
    
    
    //main.c
    #include "wrapper.h"
    
    void main(void)
    {
    	wrapper();
    }



    Best regards,
    Dejan

Related