Solved

C and C++ libraries

Posted on 1998-10-27
2
179 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cb: unreferenced local variable 11 72
Prototype for MessageDlg in Embacadero XE 2 2 35
C++ Language error 28 186
basic hardware to learn oop advanced design patterns 3 88
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

914 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

14 Experts available now in Live!

Get 1:1 Help Now