calling c++ function from c
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: calling c++ function from c

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Location
    BANGALORE ,INDIA
    Posts
    19

    Question calling c++ function from c

    Hi,

    how to call a c++ function from a c program.my code

    abc.h file

    class one
    {
    private :
    public:
    }
    void function(char *str);

    the defination of function(char *str) is in abc.cpp file.

    now i need to call this function from a .c file.

    please help regarding this.
    Last edited by harshandu; December 17th, 2005 at 12:00 AM.

  2. #2
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,716

    Re: calling c++ function from c

    Note First Thing you Should Know the difference Between c and C++

    in C there is no class.so you can't call a c++ code to c but yes you can call you C code to C++.but as according to your code you are not doing anything which is related with C++.
    So make a simple structure and use it in your c code.and you can make a call to your .h file header file .will suggest you to go for preprocessor put your function in the your newly created h file and then call this to you in your code.

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: calling c++ function from c

    Read up on extern "C" - Using extern to Specify Linkage. Apart from the fact that Humpty told (no class constructs in C...), there is one more major difference. The name mangling/decoration in C++ makes it difficult to make calls to C++ routines from C. Note - you can ony use POD types to operate between C and C++ code. You can get to know what POD types are in this thread - Destruction??? Hope this helps. Regards.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: calling c++ function from c

    Put the function in a DLL and call it from your C program.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: calling c++ function from c

    It doesn't have to be in a DLL either, you can have a .c file and a .cpp file in the same project.

    extern "C" is used to avoid name-mangling but note it must be declared from within C++ not from within C.

    Therefore you use the __cplusplus pre-processor to determine which language you are in.
    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    then later on you must close the brace with
    #ifdef __cplusplus
    }
    #endif

    It would be better in my opinion if there was a standard pre-processor that worked for both, eg

    EXTERN_C_BEGIN

    // put stuff in here

    EXTERN_C_END

    If you did that you could have a header

    Code:
    //extern_c.h
    #ifndef EXTERN_C_H
    #define EXTERN_C_H
    #ifdef __cplusplus
    #define EXTERN_C_BEGIN extern "C" {
    #define EXTERN_C_END }
    #else
    #define EXTERN_C_BEGIN
    #define EXTERN_C_END
    #endif
    #endif /* define EXTERN_C_H */
    (This is one of the places where a #define is useful).

    The fact that a function is declared extern "C" doesn't mean it can be called from C. It must also only types that are valid in C. However contrary to belief, you may includes pointers to classes if they are only forwardly declared as structs, although you should also include the word "struct" in the function prototype. Thus:
    Code:
    int someFunction( struct someType * );
    is a valid C prototype and someType in reality could be a C++ class.

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: calling c++ function from c

    Quote Originally Posted by NMTop40
    ....is a valid C prototype and someType in reality could be a C++ class.
    with certain limitations as quoted below from C++ FAQ Lite:
    A POD type is a C++ type that has an equivalent in C, and that uses the same rules as C uses for initialization, copying, layout, and addressing.

    As an example, the C declaration struct Fred x; does not initialize the members of the Fred variable x. To make this same behavior happen in C++, Fred would need to not have any constructors. Similarly to make the C++ version of copying the same as the C version, the C++ Fred must not have overloaded the assignment operator. To make sure the other rules match, the C++ version must not have virtual functions, base classes, non-static members that are private or protected, or a destructor. It can, however, have static data members, static member functions, and non-static non-virtual member functions.

    The actual definition of a POD type is recursive and gets a little gnarly. Here's a slightly simplified definition of POD: a POD type's non-static data members must be public and can be of any of these types: bool, any numeric type including the various char variants, any enumeration type, any data-pointer type (that is, any type convertible to void*), any pointer-to-function type, or any POD type, including arrays of any of these. Note: data-pointers and pointers-to-function are okay, but pointers-to-member are not. Also note that references are not allowed. In addition, a POD type can't have constructors, virtual functions, base classes, or an overloaded assignment operator.
    Regards.

  7. #7
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: calling c++ function from c

    It's true that you could not give a C function an instance to a C++ class but you can give it an "incomplete" pointer. Then the C function could call C-style methods that take a pointer to the struct.

    Example:

    Code:
    // foo.hpp - full definition of foo
    struct foo
    {
      int publicMethod1() const;
      void publicMethod2( int ); // non-const
    
      // other stuff including private members etcf.
    };
    
    // foo.h:
    
    struct foo;
    EXTERN_C_BEGIN
    struct foo * createAFoo();
    void destroyAFoo( const struct foo * pFoo );
    int fooMethod1( const struct foo * pFoo );
    void fooMethod2( struct foo * pFoo, int x );
    
    EXTERN_C_END
    
    // foo.cpp
    #include "foo.hpp"
    #include "foo.h"
    
    // implement C++ member functions
    struct foo* getAFoo() 
    {
       return new foo;
    }
    
    void destroyAFoo( const struct foo * pFoo )
    {
       delete pFoo;
    }
    
    int fooMethod1( const struct foo * pFoo )
    {
        return pFoo->publicMethod1();
    }
    
    void fooMethod2( struct foo * pFoo, int x )
    {
        pFoo->publicMethod2( x );
    }
    
    // some C code
    #include "foo.h"
    
    int func()
    {
       struct foo * pFoo = getAFoo();
       int y = fooMethod1( pFoo );
       fooMethod2( pFoo, y );
       destroyAFoo( pFoo );
    }
    and there you have it, C code using a C++ class.
    Last edited by NMTop40; December 18th, 2005 at 06:28 AM.

  8. #8
    Join Date
    Dec 2005
    Location
    BANGALORE ,INDIA
    Posts
    19

    Smile Re: calling c++ function from c

    Hi all,

    Thanks all.My problem got solved by using extern "c" and __cplusplus.The solution given by NMTOP40 worked.Thanks a lot.

    Regards,
    Harsha.
    Last edited by harshandu; December 19th, 2005 at 04:11 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center