473,466 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

mixing C++ with C

Hi,

I'd appreciate any thoughts on mixing C++ and C code. I have a project that
uses a given C interface, the rest of the project can be either in C or C++.
What would be the recomended design pattern in this case? C++ allows for a
better design, but integrating the code with the C code isn't straight
forward, it becomes rather messy. Code readability and maintenance are
important, since the project will grow in the future. Maybe someone could
speak from personal experience about this.

Thanks,
Cristian


Jul 22 '05 #1
4 1907
Cristian Tota wrote:
Hi,

I'd appreciate any thoughts on mixing C++ and C code. I have a project
that uses a given C interface, the rest of the project can be either
in C or C++. What would be the recomended design pattern in this case?
C++ allows for a better design, but integrating the code with the C
code isn't straight forward, it becomes rather messy. Code readability
and maintenance are important, since the project will grow in the
future. Maybe someone could speak from personal experience about this.


It might be a good idea to wrap a C++ class interface around the C API,
so that this API is used directly only in small parts of your program.

Jul 22 '05 #2

"Cristian Tota" <cr***********@vion-software.ro> wrote in message
news:bq**********@ally.taide.net...
Hi,

I'd appreciate any thoughts on mixing C++ and C code. I have a project that uses a given C interface, the rest of the project can be either in C or C++. What would be the recomended design pattern in this case? C++ allows for a
better design, but integrating the code with the C code isn't straight
forward, it becomes rather messy. Code readability and maintenance are
important, since the project will grow in the future. Maybe someone could
speak from personal experience about this.


http://www.parashift.com/c++-faq-lit...c-and-cpp.html

http://www.comeaucomputing.com/techtalk/#externc

HTH,
-Mike
Jul 22 '05 #3
On Thu, 4 Dec 2003 01:17:05 +0200, "Cristian Tota"
<cr***********@vion-software.ro> wrote:
Hi,

I'd appreciate any thoughts on mixing C++ and C code. I have a project that
uses a given C interface, the rest of the project can be either in C or C++.
What would be the recomended design pattern in this case? C++ allows for a
better design, but integrating the code with the C code isn't straight
forward, it becomes rather messy. Code readability and maintenance are
important, since the project will grow in the future. Maybe someone could
speak from personal experience about this.

Thanks,
Cristian


stand-alone functions, you can wrap in

extern "C"
{
int foo(){}
}

If you need to expose classes to C, you can concatenate the class name
to the function names, make them extern "C" and pass to them an extra
static pointer to substitute the this pointer. Member variables become
file-scope static variables.
A few issues back of CUJ there was an article on state machines which
presented object oriented code techniques in C to go with it.
But, usually, for interfacing you'll probably just need interface
classes --i.e. just functions, probably static, which you can collect
into ,C files, which in turn call the C++ code.

Hope it helps.
Cheers!

Jul 22 '05 #4
Cristian Tota wrote:
I'd appreciate any thoughts on mixing C++ and C code.
I have a project that uses a given C interface.
What does that mean?
Do you mean that you have libraries implemented in C?
The rest of the project can be either in C or C++.
What would be the recommended design pattern in this case?
C++ allows for a better design
but integrating the code with the C code isn't straight forward.
If you say so.
It becomes rather messy.
It shouldn't.
Code readability and maintenance are important
since the project will grow in the future.
Try to use C++ for the "rest of the project".
Maybe someone could speak from personal experience about this.


If you have source code for your C code,
try to compile it with your C++ compiler.
It is *much* easier to link object code together
if they are all compiled with the same compiler.

If you don't have all of the C source code (just the library archives),
extern "C" can be used to *help* with linkage. Try using

extern "C" {
#include <file.h>
}

to include your C header files.

Use inline functions to define C++ [member] functions, operators
and constructors which call the "C interface" so that
no *direct* references to the C interface
appear in your application source code.

Jul 22 '05 #5

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

Similar topics

3
by: bergel | last post by:
Hello, Does anyone already have some experience in mixing AWT and Swing? Is it conceptually doable? Does the design of Swing prevent interaction between an AWT and a Swing widget? Regards,...
6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
0
by: Erik Max Francis | last post by:
Is there any prohibition against mixing different protocols within the same pickle? I don't see anything about this in the Python Library Reference and, after all, the pickle.dump function takes a...
4
by: Rudolf | last post by:
Is it possible to add a vb.net source code module to a c# project and if so how? Thanks Rudolf
1
by: Marc Cromme | last post by:
I would like to ask a question about (good ?) style and possibilities in mixing C FILE* and C++ file streams. The background is that I want to use the C libpng library from within C++, but I...
2
by: Dan | last post by:
Hi What are the dangers of mixing asp and asp.net? For the .net part of the site i will need to use the global.asax file but for the asp parts it will be using the other global. file, is there...
0
by: dhruba.bandopadhyay | last post by:
I know that it's possible to mix ASP & ASP.NET 1.1 in the same website/application so long as they are in separate frames (iframes). This also holds for mixing ASP & ASP.NET 2.0 pages. But what I...
28
by: ziman137 | last post by:
Hello all, I have a question and am seeking for some advice. I am currently working to implement an algorithmic library. Because the performance is the most important factor in later...
3
by: jason | last post by:
I've been working with C# for over a year now without touching vb.net code. I had a few light years of vb.net before that. No real vb6 or windows form experience. Suddenly, I have an assignment...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.