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

  • Thread starter Klaus Schneider
  • Start date
K

Klaus Schneider

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
}
 
R

Rolf Magnus

Klaus said:
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top