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. Advertising

  2. Klaus Schneider

    Rolf Magnus Guest

    Klaus Schneider wrote:

    > 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)) ?


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

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    441
  2. Kevin P. Fleming

    C99 structure initialization in gcc-2.95.3 vs gcc-3.3.1

    Kevin P. Fleming, Nov 6, 2003, in forum: C Programming
    Replies:
    2
    Views:
    651
    Kevin P. Fleming
    Nov 6, 2003
  3. Ignoramus3627

    GCC -- reducing compiled code size

    Ignoramus3627, Jul 23, 2007, in forum: C++
    Replies:
    5
    Views:
    430
    James Kanze
    Jul 24, 2007
  4. lander
    Replies:
    5
    Views:
    608
    bruce barker
    Mar 5, 2008
  5. catcalls
    Replies:
    18
    Views:
    5,708
    Angel
    Jan 11, 2011
Loading...

Share This Page