Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > g++ compiled C++ code called from gcc compiled C code

Reply
Thread Tools

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

 
 
Klaus Schneider
Guest
Posts: n/a
 
      12-02-2004
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
}
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      12-02-2004
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
binary to C (gcc GNU/Linux compiled machine code) catcalls C Programming 18 01-11-2011 12:04 PM
If I create a page, then it's compiled upon first request, where cani find the compiled code?? lander ASP .Net 5 03-05-2008 04:34 PM
GCC -- reducing compiled code size Ignoramus3627 C++ 5 07-24-2007 10:02 AM
gcc 2.95 and gcc 3.2 gouqizi.lvcha@gmail.com C++ 8 03-16-2005 02:34 AM
C99 structure initialization in gcc-2.95.3 vs gcc-3.3.1 Kevin P. Fleming C Programming 2 11-06-2003 05:15 AM



Advertisments