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

 

  • I read online that at cpp file, i need to declare extern "C", which I did, but I am getting error in Class declaration now:

    //C++ .h file
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    class Model_Server{
        public:
               static void Command_Handler( );
    };
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif

    error:

    Thanks,

  • 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

Related