how to handle name mangling in a shared C library

R

Randy Yates

I have a library (let's call it "mylib") that is written and compiled
using C which I would like to use with both C and C++ applications.
The problem is that if I add the "extern "C"" construct in front of
the function prototypes in mylib.h, then when mylib.o is built using
the C compiler this keyword is not recognized (since mylib.c also
includes mylib.h) and the compiler barfs with errors.

Is there a good solution to this problem?
 
K

Karl Heinz Buchegger

Randy said:
I have a library (let's call it "mylib") that is written and compiled
using C which I would like to use with both C and C++ applications.
The problem is that if I add the "extern "C"" construct in front of
the function prototypes in mylib.h, then when mylib.o is built using
the C compiler this keyword is not recognized (since mylib.c also
includes mylib.h) and the compiler barfs with errors.

Is there a good solution to this problem?
--

For cases like this every C++ compiler has a predefined macro: __cplusplus

You use it like this

#ifdef __cplusplus
extern "C" {
#endif

/* Your things here */

#ifdef __cplusplus
}
#endif
 
R

Randy Yates

Karl Heinz Buchegger said:
For cases like this every C++ compiler has a predefined macro: __cplusplus

You use it like this

#ifdef __cplusplus
extern "C" {
#endif

/* Your things here */

#ifdef __cplusplus
}
#endif

But of course. I knew there was something like this defined by the compiler
but couldn't remember the string - thank you Karl Heinz.
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top