Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C and C++ libraries

Posted on 1998-10-27
2
Medium Priority
?
192 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 800 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

927 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