?
Solved

C and C++ libraries

Posted on 1998-10-27
9
Medium Priority
?
175 Views
Last Modified: 2012-05-04
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
  • 2
  • 2
  • +3
9 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 1176264
use search and replace in your library to replace all instances of "class" with "Class", etc.

(or are keywords case insensitive?)

just a suggestion
0
 

Author Comment

by:agvenkat
ID: 1176265
hi,
  I am asking for some alternative as a lot of code used by
other C guys also need to be changed so please suggest an alternative.

regards
venkatesh

0
 
LVL 1

Expert Comment

by:willemnel
ID: 1176266
I'm afraid you'll have to rewrite (search and replace or whatever) some of the code if you want to use it in C++, since the words you're using are C++ key words and the compiler recognizes it as such.  As far as I know there's no way of getting around the compiler with this.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:Answers2000
ID: 1176267
-- Did you know you double submitted the question ???

I have answered the other copy of this question.  For some reason E-E won't let me post a answer on this version of the question.

agvenkat - If you like my answer to the other copy agvenkat you can delete this copy to get your pts back


0
 
LVL 5

Accepted Solution

by:
yonat earned 800 total points
ID: 1176268
You can wrap the C library with a facade which is C++ friendly. This means that your library reamins in C as-is (compiled using a C compiler) but that you provide an interface to it, in C, which does not contain any C++ keywords.

I'll use a trivial example to illustrate. Let's say the library contains only one function, declared in lib.h:
    int class(char* new);

You create a new C header file, CppLib.h, which contains the following declaration:
    extern "C" {
    int LibClass(char* inNew);
    }

It's corresponding CppLib.c file (compiled with the C compiler) will be:
    #include "lib.h"
    int LibClass(char* inNew)
    {
        return class(inNew);
    }

Your C++ program will #include the header CppLib.h, and not the original lib.h .

Now you have the same C library, compiled with a C compiler, but it is usable for C++ programs. All you need to write is the C++ friendly facade.

If you also have C++ keywords as fields in structs, unions or enums, the same principle apply: Declare a new struct, say, with the offensive field renamed, and let your facade translate it to the original struct when passing it on to the original library.

0
 
LVL 5

Expert Comment

by:yonat
ID: 1176269
Oops, I forgat to wrap the extern declaration...

Your CppLib.h should be:

    #ifdef __cplusplus
    extern "C" {
    #endif

    int LibClass(char* inNew);

    #ifdef __cplusplus
    }
    #endif

0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1176270
If you would like this duplicate question deleted, please respond and I will have customer service delete it.
0
 

Author Comment

by:agvenkat
ID: 1176271
thresher_shark please get the duplicate question deleted.

Thanks for your help

venkatesh

0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1176272
Oky doky, your problem has been solved :-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

765 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