[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Inserting C code into a C++ program

Posted on 2004-10-12
4
Medium Priority
?
283 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
  • 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 80 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 120 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

Technology Partners: 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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

834 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