Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Need to create a C lib - using C++ classes - is it possible

Reply
Thread Tools

Need to create a C lib - using C++ classes - is it possible

 
 
Angus
Guest
Posts: n/a
 
      05-24-2008
Hello

We have a lot of C++ code. And we need to now create a library which
can be used from C and C++. Given that we have a lot of C++ code
using classes how can we 'hide' the fact that it is C++ from C
compilers?

Can we have a C header file which uses the functionality of the C++
files and compile this into a lib file?

Can anyone give me some pointers as to how to get started?

A
 
Reply With Quote
 
 
 
 
sebastian
Guest
Posts: n/a
 
      05-24-2008
Just enclose the user code in the C linkage namespace, eg:

extern "C" {
// C code
}
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-24-2008
Angus wrote:
> Hello
>
> We have a lot of C++ code. And we need to now create a library which
> can be used from C and C++. Given that we have a lot of C++ code
> using classes how can we 'hide' the fact that it is C++ from C
> compilers?
>

You will have to declare a C interface to the the library.

> Can we have a C header file which uses the functionality of the C++
> files and compile this into a lib file?
>

Only that subset of the code that's legal C. You would have something like:

#ifdef __cplusplus
extern "C" {
#endif

/* your POD types and function declarations go here */

#ifdef __cplusplus
}
#endif

The function definitions would be compiled as C++ as part of with your
C++ library.

--
Ian Collins.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-24-2008
sebastian wrote:
> Just enclose the user code in the C linkage namespace, eg:
>
> extern "C" {
> // C code
> }


Not a lot of use if the code is C and compiled with a C compiler...

--
Ian Collins.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-24-2008
Chris Thomasson wrote:
> "Angus" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hello
>>
>> We have a lot of C++ code. And we need to now create a library which
>> can be used from C and C++. Given that we have a lot of C++ code
>> using classes how can we 'hide' the fact that it is C++ from C
>> compilers?
>>
>> Can we have a C header file which uses the functionality of the C++
>> files and compile this into a lib file?
>>
>> Can anyone give me some pointers as to how to get started?

>
> You can try something like this:
>
> <pseudo-code sketch>
> __________________________________________________ ______________
>
>
> /* my_class.h
> -------------------------------------------------------------*/
> #ifndef MY_CLASS_H
> #define MY_CLASS_H
> #ifdef __cplusplus
> # define EXTERN extern "C"
> #else
> # define EXTERN extern
> #endif
>

Why go to all that trouble when you can just write

#ifdef __cplusplus
extern "C" {
#endif

typedef struct my_class my_class_type;

int my_class_create(my_class_type**);
int my_class_destroy(my_class_type*);
int my_class_do_something(my_class_type*);

#ifdef __cplusplus
}
#endif

Then you don't have to mess about with casts in the C++ code.

--
Ian Collins.
 
Reply With Quote
 
Angus
Guest
Posts: n/a
 
      05-24-2008
On 24 May, 22:46, Ian Collins <(E-Mail Removed)> wrote:
> Chris Thomasson wrote:
> > "Angus" <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed)...
> >> Hello

>
> >> We have a lot of C++ code. *And we need to now create a library which
> >> can be used from C and C++. *Given that we have a lot of C++ code
> >> using classes how can we 'hide' the fact that it is C++ from C
> >> compilers?

>
> >> Can we have a C header file which uses the functionality of the C++
> >> files and compile this into a lib file?

>
> >> Can anyone give me some pointers as to how to get started?

>
> > You can try something like this:

>
> > <pseudo-code sketch>
> > __________________________________________________ ______________

>
> > /* my_class.h
> > -------------------------------------------------------------*/
> > #ifndef MY_CLASS_H
> > #define MY_CLASS_H
> > #ifdef __cplusplus
> > # define EXTERN extern "C"
> > #else
> > # define EXTERN extern
> > #endif

>
> Why go to all that trouble when you can just write
>
> #ifdef *__cplusplus
> extern "C" {
> #endif
>
> typedef struct my_class my_class_type;
>
> int my_class_create(my_class_type**);
> int my_class_destroy(my_class_type*);
> int my_class_do_something(my_class_type*);
>
> #ifdef *__cplusplus}
>
> #endif
>
> Then you don't have to mess about with casts in the C++ code.
>
> --
> Ian Collins.- Hide quoted text -
>
> - Show quoted text -


Yes the

#ifdef __cplusplus
extern "C" {
#endif

worked.

I thought this would be harder.

Not often I say that...

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-25-2008
Alf P. Steinbach wrote:
> * Angus:
>> Hello
>>
>> We have a lot of C++ code. And we need to now create a library which
>> can be used from C and C++. Given that we have a lot of C++ code
>> using classes how can we 'hide' the fact that it is C++ from C
>> compilers?

>
> The C++ code will need the C++ runtime library.
>
> Within the standards of C and C++ the only way to achieve that is to
> insist that the C code using the library is called from a C++ main program.
>

Is it? In practice, the only constraint on any platform I've used is
that the application must be linked with the C++ compiler diver.

> The best is to forget that silly idea. Using C library from C++, OK.
> But C++ has additional requirements from runtime library, so other way,
> generally !OK, unless you're working at a low level where you wouldn't
> have to ask...
>

Why is it generally not OK? Provided the restrictions regarding
exception leakage are met, there shouldn't be a problem.

--
Ian Collins.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-25-2008
Alf P. Steinbach wrote:
> * Ian Collins:
>> Alf P. Steinbach wrote:
>>> * Angus:
>>>> Hello
>>>>
>>>> We have a lot of C++ code. And we need to now create a library which
>>>> can be used from C and C++. Given that we have a lot of C++ code
>>>> using classes how can we 'hide' the fact that it is C++ from C
>>>> compilers?
>>> The C++ code will need the C++ runtime library.
>>>
>>> Within the standards of C and C++ the only way to achieve that is to
>>> insist that the C code using the library is called from a C++ main
>>> program.
>>>

>> Is it? In practice, the only constraint on any platform I've used is
>> that the application must be linked with the C++ compiler diver.

>
> It's not clear exactly what you mean, but I'm guessing you mean using a
> C++ compiler and linker for the main program.
>

Ah! parse error, main() != main program!

>>> The best is to forget that silly idea. Using C library from C++, OK.
>>> But C++ has additional requirements from runtime library, so other way,
>>> generally !OK, unless you're working at a low level where you wouldn't
>>> have to ask...
>>>

>> Why is it generally not OK? Provided the restrictions regarding
>> exception leakage are met, there shouldn't be a problem.

>
> E.g. static variables of class type, internal use of exceptions, use of
> standard library features that (implementation-specific) requires C++
> runtime library, including internal use of exceptions in standard
> library; this is a FAQ, IIRC.
>

True, but linking the application with the C++ compiler diver takes care
of these details. But as you say, this makes a C++ application, even if
most of the code is C!

--
Ian Collins.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-25-2008
On May 24, 10:27 pm, Angus <(E-Mail Removed)> wrote:
> We have a lot of C++ code. And we need to now create a
> library which can be used from C and C++. Given that we have
> a lot of C++ code using classes how can we 'hide' the fact
> that it is C++ from C compilers?


You can hide the fact from the C compilers; just make sure that
everything in the header files used by the C compilers is
`extern "C"'. That doesn't guarantee that it's going to work;
formally, using C++ requires that the function main be written
and compiled in C++. (Practically, this is usually only a
problem if you have variables with static lifetime which have
dynamic initialization. It's possible too that everything will
work fine if you follow certain implementation defined rules for
linking: maybe using g++ to link, rather than gcc, or maybe use
dynamic linking, instead of static. You'll have to enquire
specifically concerning your implementation for that, however.)

> Can we have a C header file which uses the functionality of
> the C++ files and compile this into a lib file?


> Can anyone give me some pointers as to how to get started?


The only real problems are ensuring that the header files can be
compiled by both compilers, and ensuring that either main is
written in C++, or you've fulfilled whatever conditions your
implementation sets to ensure correct initialization of statics.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-25-2008
On May 24, 10:41 pm, Ian Collins <(E-Mail Removed)> wrote:
> sebastian wrote:
> > Just enclose the user code in the C linkage namespace, eg:


> > extern "C" {
> > // C code
> > }


> Not a lot of use if the code is C and compiled with a C compiler...


In the header file, this has to be conditional, and only present
if the compiler is a C++ compiler. And of course, the library
source files must be compiled with the C++ compiler. The
classical solution is to use something like:

#ifdef __cplusplus
extern "C" {
#endif
// Usual contents here...
#ifdef __cplusplus
}
#endif

It's ugly, but it works.

Also, of course, you have to ensure that the "usual contents"
are all acceptable and mean the same thing to both the C and the
C++ compiler. You can freely use classes, for example
(including classes with virtual functions, et al.), but you
cannot define them in a header which will be used by the C code;
the most you can do is declare them, e.g.:

struct MyClass ;

, and because this results in an incomplete type, any use in the
header is restricted to pointers and the like.

Similarly, you can't use references in the interface exposed to
C.

One frequent solution is to define a class, exactly as one would
in C++, and then provide a set of wrapper functions, something
along the lines of:

#ifdef __cplusplus
extern "C" {
#endif
struct MyClass ;

MyClass* createMyClass(...) ;
void doSomethingWithMyClass( MyClass*, ... ) ;
// ...
#ifdef __cplusplus
}
#endif

The functions then just forward to the class members.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lib depending on Lib, packaging for distro Christopher C++ 4 11-01-2007 10:36 PM
difference between libboost_regex-mt-gd-1_33_1.lib and boost_regex-mt-gd-1_33_1.lib ideal.black@gmail.com C++ 3 09-30-2007 06:54 AM
how to debug this error? /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crt1.o Durduran C Programming 10 07-30-2007 09:03 PM
Need odbc32.lib odbccp32.lib Praetorian C++ 1 04-20-2006 07:14 PM
Diff betw common/lib and shared/lib in Tomcat James Yong Java 0 09-12-2005 02:36 AM



Advertisments