Solved

C and C++ libraries

Posted on 1998-10-27
2
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to clear a vector as well as how to detect empty vectors in C++.

636 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