473,856 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Jun 27 '08 #1
18 2306
Just enclose the user code in the C linkage namespace, eg:

extern "C" {
// C code
}
Jun 27 '08 #2
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.
Jun 27 '08 #3
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.
Jun 27 '08 #4
Ian Collins <ia******@hotma il.comkirjutas:
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.
To OP: beware that the extern "C" functions are not allowed to leak any
exceptions - this is kind of natural as the calling C client will not
have any means to deal with them. I would define a couple of macros to
put in the beginning and end of each extern "C" function, which
essentially do "catch(...) " and convert the exceptions into corresponding
C error codes or into whatever appropriate.

hth
Paavo

Jun 27 '08 #5
"Angus" <an*********@gm ail.comwrote in message
news:3c******** *************** ***********@p25 g2000hsf.google groups.com...
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

typedef void* my_class_type;

EXTERN int my_class_create (my_class_type* )
EXTERN int my_class_destro y(my_class_type );
EXTERN int my_class_do_som ething(my_class _type);

#endif

/* my_class.hpp
-------------------------------------------------------------*/
#ifndef MY_CLASS_HPP
#define MY_CLASS_HPP

class my_class {
public:
my_class();
~my_class() throw();
void do_something();
};

#endif

/* my_class.cpp
-------------------------------------------------------------*/
#include "my_class.h "
#include "my_class.h pp"
#include <cstdio>
my_class::my_cl ass() {
std::printf("(% p)->my_class::my_c lass()\n", (void*)this);
}
my_class::~my_c lass() throw() {
std::printf("(% p)->my_class::~my_ class()\n", (void*)this);
}
void my_class::do_so mething() {
std::printf("(% p)->my_class::do_s omething()\n", (void*)this);
}

int my_class_create (my_class_type* _pcthis) {
try {
*_pcthis = new my_class;
} catch(...) {
return -1;
}
return 0;
}
int my_class_destro y(my_class_type _cthis) {
my_class* const _this = (my_class*)_cth is;
try {
delete _this;
} catch(...) {
return -1;
}
return 0;
}
int my_class_do_som ething(my_class _type _cthis) {
my_class* const _this = (my_class*)_cth is;
try {
_this->do_something() ;
} catch(...) {
return -1;
}
return 0;
}
_______________ _______________ _______________ _______________ ____


Now you can use it from C like:
_______________ _______________ _______________ _______________ ____
#include "my_class.h "
int main(void) {
my_class_type _this;
my_class_create (&_this);
my_class_do_som ething(_this);
my_class_destro y(_this);
return 0;
}

_______________ _______________ _______________ _______________ ____


Any thoughts?

Jun 27 '08 #6
Chris Thomasson wrote:
"Angus" <an*********@gm ail.comwrote in message
news:3c******** *************** ***********@p25 g2000hsf.google groups.com...
>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_destro y(my_class_type *);
int my_class_do_som ething(my_class _type*);

#ifdef __cplusplus
}
#endif

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

--
Ian Collins.
Jun 27 '08 #7
On 24 May, 22:46, Ian Collins <ian-n...@hotmail.co mwrote:
Chris Thomasson wrote:
"Angus" <anguscom...@gm ail.comwrote in message
news:3c******** *************** ***********@p25 g2000hsf.google groups.com...
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_destro y(my_class_type *);
int my_class_do_som ething(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...

Jun 27 '08 #8
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.
Jun 27 '08 #9
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.
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2659
by: Beach Potato | last post by:
Dear Y'all: I'm about to start porting a big old project written in anscient version of Delphi to something more stable, robust, supportable and maybe even portable. Since I haven't seriously touched C for large implementations, I'm seeking advice on what to use for development. My ultimate goal is to spend as less time on it as possible. I'll be writing it in Windows 32-bit environment, probably Win2000 or Win98. Planning to use a...
2
1426
by: Francesc Guim Bernat | last post by:
Dear Collegues, I've developed an application with .NET framework using System.Xml utilities. But now I can verify that my applications works more slower than i expected. I think that it's because .NET applications runs in ..NET virtual machine :-( . It's possible to make a solution where the executable code don't run in this virtual machine ? I want to creat an exectuable for architectures Win32 and Win64 but using System.Xml utilities.
5
2220
by: Carl Bevil | last post by:
I'm creating a library for internal use that relies on third-party code. I don't want clients of this library to know anything about the third party code, when compiling or running. Generally I've been achieving this by having an abstract base class which defines an interface, and inheriting a concrete class which defines the implementation. Clients of the library deal only with the base class and request objects of that type from a...
1
1314
by: Julia | last post by:
Hi I have a domain model and I am looking for the correct design patterns to use, The following is my domain model Server-> the is the thread boundaries,the server create a thread the Thread create the Manager ,the Manager create the MessagingService and
7
1440
by: stephane | last post by:
Hi, I must implement the method printLn of the class Thoraire. And I've done this, can someone tell me if I am right ?
3
5927
by: Davide Bedin | last post by:
I have a "library" schema with the simple and complex types I commonly use in other schemas and then several other schemas, maybe created by other developers, that import/include the library schema. And then other schemas can extend it..... The classes created by XSD really help to make the development faster and easier. I really would like to use XSD.exe tool as much as possible and to avoid editing the classes defined by the tool. ...
48
3262
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and "huge" Indy is both as a project, in support, development, and use. But Indy is new to the .net world. Indy is a HUGE library implementing over 120 internet protocols and standards and comes with complete source. Its an open source project, but not...
5
3490
by: Benne Smith | last post by:
Hi, I have three enviroments; a development, a testing and a production enviroment. I'm making a big application (.exe), which uses alot of different webservices. I don't use the webservices by adding a WebReference, since it does not allow me to keep state (cookiecontainer) or to specify functions on the classes (like if i want to override the ToString() function on a class from my webservice). So the only way i can see how i can get...
3
4419
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here really understand how this could be an issue. The assemblies that the system is complaining about are ones that we build here and we're not changing version numbers on anything. The errors come and go with no apparent rhyme or reason. We do not...
0
9904
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10692
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9527
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7928
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7086
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5754
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4169
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.