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

 

Parents
  • Hi,

    I was able to mitigate the cpp file error by having the Class  declaration & static class member function in the same file,

    But when I call the static class member function from C file, I am getting the error:

  • Hi,

    Could you please show how your code looks like now that everything is in the same file?

    Could you also show how you call static class member function from C file?

    Best regards,
    Dejan

  • Hello ,

    Thanks I was able to work it out.

    Was because I had to mark C++ include files with extern "C".


    This error was because I shouldn't call C++ symbols like class names directly in C file, had to write a C++ wrapper file then call wrapper APIs into C file.


    But you can help me with one thing,
    That is, I want to write my model handlers in C++ and the assign those C++ model handlers to model handler structure. like below:

    static const struct bt_mesh_health_srv_cb health_srv_cb = {
    	.attn_on = attention_on, //C++ function
    	.attn_off = attention_off,//C++ function
    };


    Can you suggest how to do this..?

    If you have a reference code, then please point me.



    Thanks,

  • Hi,

    C++ methods cannot be used directly as callbacks because of the need for having a class instance. However, static functions should be able to run even if they are defined in C++ file.
    When mixing C and C++, 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. You could take a look at this discussion. You can also read more here.

    Best regards,
    Dejan

  • 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,

Reply
  • 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,

Children
  • 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