Solved

C and C++ libraries

Posted on 1998-10-27
2
177 Views
Last Modified: 2010-04-02
I have some libraries developed in C. These libs use variables names 'class' , 'new' etc which are keywords in C++. Please suggest me any option where I can use this libs in C++ without rewriting the libaries.

0
Comment
Question by:agvenkat
2 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 200 total points
ID: 1176273
1. search and replace in the library:  As already suggested (this much is obvious)

"class"  --- becomes "_Class"
"new"    --- becomes "_New"
etc.  (or whatever variation you like on the naming)


2. The programmers who use the library from C - include a header file right - modify this header with something like: (insert this at the top)

#ifndef __cplusplus
#define class  _Class
#define new    _New
/* etc.. */
#endif

The compiler defines __cplusplus automatically when compiling C++ programs, but not when compiling C.  The macros hence allow use of the original names in C programs only.

The C programs will have to recompile their programs (they'd have to do this anyway to use a new version of the library), but will NOT need to make any source changes.

Finally if there are different headers, and some C programs include one, some include others, etc... stick this code into a new header, and then #include from the top of all your real headers:-

#ifndef BACKWARDCOMPATIBLE
#define BACKWARDCOMPATIBLE

#ifndef __cplusplus
#define class  _Class
#define new    _New
/* etc.. */
#endif

#endif


4. Modify all library source code (global search and replace) to use the new names if you want the library to compile in C++.  Make sure the headers that the C programs use only have C style code.  Also make sure that any functions have extern "C" style linkage

5. Rebuild the library

- C programs recompile with the library,   They can use either the old or the new names
- New C++ programs,  must use the new names
- The library uses the new names internally
0
 
LVL 7

Expert Comment

by:linda101698
ID: 1176274
I added the 200 points deducted from your account by the duplicate question back to your account.  Since Answers2000 had actually posted the correct answer first, I felt that he/she should get the credit for their answer.

Linda Gardner
Customer Service @ Experts Exchange
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now