473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to handle name mangling in a shared C library

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?
--
Randy Yates
Sony Ericsson Mobile Communications
Research Triangle Park, NC, USA
ra*********@sonyericsson.com, 919-472-1124
Nov 14 '05 #1
2 1772
Randy Yates wrote:

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

--
Karl Heinz Buchegger
kb******@gascad.at
Nov 14 '05 #2
Karl Heinz Buchegger <kb******@gascad.at> writes:
Randy Yates wrote:

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


But of course. I knew there was something like this defined by the compiler
but couldn't remember the string - thank you Karl Heinz.
--
Randy Yates
Sony Ericsson Mobile Communications
Research Triangle Park, NC, USA
ra*********@sonyericsson.com, 919-472-1124
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tim Slattery | last post by:
Does the C++ language standard mandate a particular name-mangling method? I'm trying to use the Entrust toolkit to create a C++ program that encrypts and decrypts files. Entrust supplies header...
20
by: Randy Yates | last post by:
Why is this necessary? The identifiers provide by the programmer are unique, and that is what is important - why map what the user has specified to something else? -- Randy Yates Sony Ericsson...
2
by: Randy Yates | last post by:
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...
6
by: yyy | last post by:
my question is rather theoretical than practical in the pure programming aspect... is "name mangling" the same as "name decorating" ? browsing the web, you might find multiple people saying...
1
by: noleander | last post by:
I need to use a library supplied by someone else: libjpeg.lib. This is a plain C library. I do not have the source code. I do have the header *.h files. When I run dumpbin on the libjpeg.lib,...
6
by: David Wade | last post by:
Folks, Does any one know of any "name mangling" software that allows standard C with long names to be linked? Any suggestions for a better place to look? I have tried putting various searchs...
5
by: Subhransu Sahoo | last post by:
Hi All, Does the C++ standard tell function overloading can't be done with the return types of two functions ? If so, is it true that the name mangling scheme does not take the return type into...
8
by: sam_cit | last post by:
Hi Everyone, I read somwhere that c++ compiler does name mangling of functions which is why c source code can't invoke functions from object files that were generated using c++ compiler. Can...
4
by: jason.cipriani | last post by:
Does anybody know if C++0x is going to change C++ name mangling at all? Such as, standardizing name mangling instead of leaving it up to the compiler? Jason
14
by: Megalo | last post by:
why not make "name mangling" of C++ standard so should be possible to call the classes and the functions of C++ from other C++ compiler thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.