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
Solved

C and C++ libraries

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.

808 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