f



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

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
}
0
klaus5137 (1)
12/2/2004 1:05:58 PM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

1 Replies
1183 Views

Similar Articles

[PageSpeed] 6

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.

0
ramagnus (3487)
12/2/2004 1:44:09 PM
Reply: