Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 609315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:33:12+00:00 2026-05-13T17:33:12+00:00

I am trying to access a C++ class and call its method from a

  • 0

I am trying to access a C++ class and call its method from a .c file.

I google this topic and find this http://developers.sun.com/solaris/articles/mixing.html

It says:

You can write extern "C" functions in C++ that access class M objects and call them from C code.

Here is a C++ function designed to call the member function foo:

extern "C" int call_M_foo(M* m, int i) { return m->foo(i); }

My question is where do I put the about line? In my C++ .h file? Or C .h file?

And it goes on and says:

Here is an example of C code that uses class M:

struct M;                       // you can supply only an incomplete declaration

int call_M_foo(struct M*, int); // declare the wrapper function

int f(struct M* p, int j)       // now you can call M::foo
{
  return call_M_foo(p, j);
}

But how/where do I create the class M in my C file?
And where do I put the above code? C .h file? C++ .h file? Or C .c file?

Thank you.

Thank you for GMan‘s detailed answer.
I did follow your suggestion. But I get compile error in my .c file.

main.c:33:
./some_class.h:24: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘’ token
./some_class.h:25: error: expected ‘)’ before ‘
’ token
./some_class.h:26: error: expected ‘)’ before ‘*’ token

And here are my some_class.h line 24-26:

#ifdef __cplusplus 
class M {

public:
  M();
  virtual ~M(); 

  void method1(char* name, char* msg);
};

extern "C" {
#else
struct M;
#endif

  /* access functions line 24-26 are here*/ 
  M* M_new(void);
  void M_delete(M*);
  void M_method1(M*, char*, char*);
#ifdef __cplusplus 
}
#endif

For some reason, my C compiler does not like extern "C" in GMan‘s original some_test.h. So I have to modify to above. It seems like the C compiler does not like/understand the struct M; line.

Any idea will be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:33:12+00:00Added an answer on May 13, 2026 at 5:33 pm

    Your header file, which is shared between your C and C++ code:

    #ifdef __cplusplus // only actually define the class if this is C++
    
    class some_class
    {
        public:
            int some_method(float);
    };
    
    #else
    
    // C doesn't know about classes, just say it's a struct
    typedef struct some_class some_class; 
    
    #endif
    
    // access functions
    #ifdef __cplusplus
        #define EXPORT_C extern "C"
    #else
        #define EXPORT_C
    #endif
    
    EXPORT_C some_class* some_class_new(void);
    EXPORT_C void some_class_delete(some_class*);
    EXPORT_C int some_class_some_method(some_class*, float);
    

    Then your source file:

    #include "some_foo.h"
    
    int some_class::some_method(float f)
    {
        return static_cast<int>(f);
    }
    
    // access functions
    EXPORT_C some_class* some_class_new(void)
    {
        return new some_class();
    }
    
    EXPORT_C void some_class_delete(some_class* this)
    {
        delete this;
    }
    
    EXPORT_C int some_class_some_method(some_class* this, float f)
    {
        return this->some_method(f);
    }
    

    Now compile that source, and link to it. Your C source would be something like:

    #include "some_class.h"
    
    some_class* myInstance = some_class_new();
    
    int i = some_class_some_method(myInstance, 10.0f);
    
    some_class_delete(myInstance);
    

    If you’re serious about mixing C and C++, you’ll want macro’s.


    Here are some sample macro’s that would make this much easier:

    // in something like c_export.h
    // extern "C" macro
    #ifdef __cplusplus
        #define EXPORT_C extern "C"
    #else
        #define EXPORT_C
    #endif
    
    // new
    #define EXPORT_C_CLASS_NEW(classname) EXPORT_C \
                classname * classname##_new(void)
    
    #define EXPORT_C_CLASS_NEW_DEFINE(classname) \
                EXPORT_C_CLASS_NEW(classname) \
                { return new classname (); }
    
    // repeat as much as you want. allows passing parameters to the constructor
    #define EXPORT_C_CLASS_NEW_1(classname, param1) EXPORT_C \
                classname * classname##_new( param1 p1)
    
    #define EXPORT_C_CLASS_NEW_1_DEFINE(classname, param1) \
                EXPORT_C_CLASS_NEW_1(classname, param1) \
                { return new classname (p1); }
    
    // delete
    #define EXPORT_C_CLASS_DELETE(classname) EXPORT_C \
                void classname##_delete( classname * this)
    
    #define EXPORT_C_CLASS_DELETE_DEFINE(classname) \
                EXPORT_C_CLASS_DELETE(classname) \
                { delete this; }
    
    // functions
    #define EXPORT_C_CLASS_METHOD(classname, methodname, ret) EXPORT_C \
                ret classname##_##methodname##( classname * this)
    
    #define EXPORT_C_CLASS_METHOD_DEFINE(classname, methodname, ret) \
                EXPORT_C_CLASS_METHOD(classname, methodname, ret) \
                { return this->##methodname##(); }
    
    // and repeat as necessary.
    #define EXPORT_C_CLASS_METHOD_1(classname, methodname, ret, param1) EXPORT_C \
                ret classname##_##methodname( classname * this, param1 p1)
    
    #define EXPORT_C_CLASS_METHOD_1_DEFINE(classname, methodname, ret, param1) \
                EXPORT_C_CLASS_METHOD_1(classname, methodname, ret, param1) \
                { return this->##methodname##(p1); }
    

    And so on. Our header/source becomes:

    // header
    #include "c_export.h" // utility macros
    
    #ifdef __cplusplus // only actually define the class if this is C++
    
    class some_class
    {
        public:
            int some_method(float);
    };
    
    #else
    
    // C doesn't know about classes, just say it's a struct
    typedef struct some_class some_class; 
    
    #endif
    
    // access functions
    EXPORT_C_CLASS_NEW(some_class);
    EXPORT_C_CLASS_DELETE(some_class);
    EXPORT_C_CLASS_METHOD_1(some_class, some_method, int, float);
    
    // source
    #include "some_foo.h"
    
    int some_class::some_method(float f)
    {
        return static_cast<int>(f);
    }
    
    // access functions
    EXPORT_C_CLASS_NEW_DEFINE(some_class);
    EXPORT_C_CLASS_DELETE_DEFINE(some_class);
    EXPORT_C_CLASS_METHOD_1_DEFINE(some_class, some_method, int, float);
    

    And that’s much more concise. It could be made simpler (possibly) with variadic macro’s, but that’s non-standard and I leave that to you. :] Also, you can make macro’s for normal non-member functions.


    Note that C does not know what references are. If you want to bind to a reference, your best bet is probably just to write the export definition manually. (But I’ll think about it, maybe we can get it automatically).

    Imagine our some_class took the float by (non-const)reference (for whatever reason). We’d define the function like so:

    // header
    // pass by pointer!                                     v
    EXPORT_C_CLASS_METHOD_1(some_class, some_method, int, float*) ;
    
    // source
    EXPORT_C_CLASS_METHOD_1(some_class, some_method, int, float*) 
    {
        // dereference pointer; now can be used as reference
        return this->some_method(*p1);
    }
    

    And there we go. C would interface with references with pointers instead:

    // c source, if some_method took a reference:
    float f = 10.0f;
    int i = some_class_some_method(myInstance, &f);
    

    And we pass f “by reference”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 501k
  • Answers 501k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should consider doing this in a validation or submit… May 16, 2026 at 2:11 pm
  • Editorial Team
    Editorial Team added an answer It defines that the current action will respond to various… May 16, 2026 at 2:11 pm
  • Editorial Team
    Editorial Team added an answer I've used these techniques before and they both work well.… May 16, 2026 at 2:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm trying to implement generic method to put in a class a calculated value
Note : Part of a series: C#: Accessing form members from another class and
I'm trying to write a helper class that represents fields on an object. The
Trying to figure out out how to overload parenthesis on a class. I have
I am trying to write a class template that provides a comparison operator between
I am trying to figure out how to use the dom to access the
I have a method defined in my controller that I am trying to create
I'm trying to add a web reference to an external Axis - generated web
I have a strange issue! I am trying to remove an event listener on
I'm fairly new to Rails and am trying to figure out how to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.