Solved

Inserting C code into a C++ program

Posted on 2004-10-12
4
248 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 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 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.

776 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