g++ compiled C++ code called from gcc compiled C code

Discussion in 'C++' started by Klaus Schneider, Dec 2, 2004.

  1. Hi all!

    In a C library compiled with gcc there is a pointer to a function defined
    and called from within the library. Now I'm using that library in a C++
    project and I'd like to set this function pointer to a C++ function. Do I
    have to set the C++ function __attribute__((cdecl)) ? Are there any other
    things I have to worry about? See example code below.

    Thanks very much,
    Klaus


    Example Code:

    -----------------------------------------------------
    *** lib.c (compiled with gcc and linked to a library):

    void (* logfunc)(int,char*,...);

    int getSomething(void)
    {
    ...
    logfunc(LOG_ERROR,"Some message: %s\n",reason);
    ...
    }


    -----------------------------------------------------
    *** gui.c my C++ code using the library

    extern "C" {
    int getSomething(void); // prototype of library function
    }

    // my C++ function which will be called by the library
    void guilog(int type,char* format,...); // need __attribute__ ((cdecl)) ??


    int main(void)
    {
    logfunc = guilog;
    ...
    val = getSomething();
    ...
    }

    void guilog(int type,char* format,...)
    {
    ... // print error message in GUI window
    }
     
    Klaus Schneider, Dec 2, 2004
    #1
    1. Advertisements

  2. Klaus Schneider

    Rolf Magnus Guest

    I don't know what the attribute cdecl does, but ususally, making it extern
    "C" is enough.
     
    Rolf Magnus, Dec 2, 2004
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.