using old c libraries

C

cppaddict

Hi,

I want to write a C++ program, but still have access to some C code
which has a bunch of utility functions I'd like to use.

My question is: Compiling with gcc, how difficult will this be? What
do I have to do to make it work?

Thanks for any comments or references,
cppaddict
 
R

Rakesh Kumar

The three library headers that you may want to change in C++ are -

<stdio.h> to <cstdio>
<math.h> to <cmath>
<stdlib.h> to <cstdlib>

as they are the main differences between C++ and C in terms of
libraries.
Other than that, I guess things would be pretty fine. clc++ can
please comment on this, better.
 
J

John Harrison

cppaddict said:
Hi,

I want to write a C++ program, but still have access to some C code
which has a bunch of utility functions I'd like to use.

My question is: Compiling with gcc, how difficult will this be? What
do I have to do to make it work?

Thanks for any comments or references,
cppaddict

You need to make sure that your C functions have C linkage. This is
something C++ understands but C doesn't, so you need to code your headers
something like this

#ifdef __cplusplus
extern "C" { // C linkage
#endif

void some_library_function(void);
int another_library_function(int, int);

#ifdef __cplusplus
} // end C linkage
#endif

The name __cplusplus will be defined when you are using a C++ compiler but
not when you are using a C compiler.

Also note use of '(void)'. C understands () as a function with a variable
number of parameters, whereas C++ understands that as a function with zero
parameters, so use (void) which both understand.

john
 
B

Brian Rodenborn

Also note use of '(void)'. C understands () as a function with a variable
number of parameters, whereas C++ understands that as a function with zero
parameters, so use (void) which both understand.


I think you mean, "a fixed but unspecified number of parameters."

This is in declarations, not definitions.



Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top