• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

C and C++ libraries

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
agvenkat
Asked:
agvenkat
  • 2
  • 2
  • 2
  • +3
1 Solution
 
scrapdogCommented:
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
 
agvenkatAuthor Commented:
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
 
willemnelCommented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Answers2000Commented:
-- 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
 
yonatCommented:
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
 
yonatCommented:
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
 
thresher_sharkCommented:
If you would like this duplicate question deleted, please respond and I will have customer service delete it.
0
 
agvenkatAuthor Commented:
thresher_shark please get the duplicate question deleted.

Thanks for your help

venkatesh

0
 
thresher_sharkCommented:
Oky doky, your problem has been solved :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now