Solved

Inserting C code into a C++ program

Posted on 2004-10-12
4
267 Views
Last Modified: 2010-04-01
I am working in C++.  We downloaded some C code from ITU that implements some compression algorithms.  I am rather new to C++ and I want to know what is the right approach to using C code in C++.

Should I translate it all into C++ classes?
Is there a common alternate approach?

All of the code will be compiled together on Linux using g++ 3.2.x.
0
Comment
Question by:klopex
[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
4 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 12287755
To use them as they are you need to change the function declaration to be extern "C":
in the header file add:

extern "C" {

.. all function prototypes.

}
0
 
LVL 13

Assisted Solution

by:SteH
SteH earned 20 total points
ID: 12287785
Whether you need to translate it to C++ depends on your preferences and the needs of the application. Advantage of the C approach: code is tested, you don't need to do it (as thoroughly). C++ approach: full OO support, encapsulation. You have more control on how data is changed and who can use it in which way.
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 30 total points
ID: 12287849
Hi klopex,
I think the most feasible approach would be to create C++ wrapper classes which call the C API.

By this, you have a nicely encapsulated compressor state, or can use exceptions, without the danger of a C->C++ migration. And additionally, you can easily exchange the C part when there is a new version.

Cheers!

Stefan
0
 
LVL 3

Author Comment

by:klopex
ID: 12289310
Thank You.  I had been considering writing the wrapper class so I wouldn't have to edit good code.  I think that is what I am going to do...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 72
Better understanding on C++ Class serialization and formats 9 103
object oriented programming comparison 5 93
learn programming 8 94
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

737 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