473,383 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Using C functions in a C++ program

A colleague of mine who is a C developer wrote several functions in C
which I now need to invoke in my C++ application. This is normally not
a problem except that the header file that he wrote contains also
several structures and #defines, which I need to use in my C++
application.

What is the best way to use the C functions, structures and defines in
my code without upsetting the compiler / linker?

For the C functions I know I need to use extern "C" { ...}. But how do
I get to include the structures and the defines? If I include the
header file in my C++ code, then the linker will fail.

Any help is greatly appreciated.

Thanks.

Aug 22 '06 #1
11 2552

ruffiano wrote:
A colleague of mine who is a C developer wrote several functions in C
which I now need to invoke in my C++ application. This is normally not
a problem except that the header file that he wrote contains also
several structures and #defines, which I need to use in my C++
application.

What is the best way to use the C functions, structures and defines in
my code without upsetting the compiler / linker?

For the C functions I know I need to use extern "C" { ...}. But how do
I get to include the structures and the defines? If I include the
header file in my C++ code, then the linker will fail.
You can embed the C functions and structures in a namespace:

namespace CFunctions {

#include "CHeader.h"

};

Now you refer to them as CFunctions::function() and such and it won't
clobber your global namespace with names you already have.

You can also use preprocessor stuff to undefine and redefine your
macros where needed. I don't know if push/pop and undef are standard
but they are rather common.

Aug 22 '06 #2
ruffiano posted:
A colleague of mine who is a C developer wrote several functions in C
which I now need to invoke in my C++ application. This is normally not
a problem except that the header file that he wrote contains also
several structures and #defines, which I need to use in my C++
application.

You'll have your files like:

apple.hpp
table.hpp

apple.cpp
table.cpp
disk.cpp

Your colleague's source files should have the extension ".c" -- most
compilers take this to mean that it should be compiled as C. Make sure though
that you go through the compiler options, paying specific attention to
whether it's compiled as C89 or as C99.

What is the best way to use the C functions, structures and defines in
my code without upsetting the compiler / linker?
For the C functions I know I need to use extern "C" { ...}. But how do
I get to include the structures and the defines? If I include the
header file in my C++ code, then the linker will fail.

Why does the linker fail?

--

Frederick Gotham
Aug 22 '06 #3
Noah Roberts wrote:
ruffiano wrote:
>>A colleague of mine who is a C developer wrote several functions in C
which I now need to invoke in my C++ application. This is normally not
a problem except that the header file that he wrote contains also
several structures and #defines, which I need to use in my C++
application.

What is the best way to use the C functions, structures and defines in
my code without upsetting the compiler / linker?

For the C functions I know I need to use extern "C" { ...}. But how do
I get to include the structures and the defines? If I include the
header file in my C++ code, then the linker will fail.


You can embed the C functions and structures in a namespace:

namespace CFunctions {

#include "CHeader.h"

};

Now you refer to them as CFunctions::function() and such and it won't
clobber your global namespace with names you already have.
But then they will fail to link, assuming the C code is compiled with a
C compiler, all the symbols will be extern "C", not C++ mangled names.

Even if the C code is compiled with the C++ compiler, you will have to
conditionally include the namespace declaration for C++ compilation.

Better off just putting extern "C" round the inclusion of the header, or
better still conditionally included the wrapper in the header.

--
Ian Collins.
Aug 22 '06 #4

Ian Collins wrote:
Noah Roberts wrote:
ruffiano wrote:
>A colleague of mine who is a C developer wrote several functions in C
which I now need to invoke in my C++ application. This is normally not
a problem except that the header file that he wrote contains also
several structures and #defines, which I need to use in my C++
application.

What is the best way to use the C functions, structures and defines in
my code without upsetting the compiler / linker?

For the C functions I know I need to use extern "C" { ...}. But how do
I get to include the structures and the defines? If I include the
header file in my C++ code, then the linker will fail.

You can embed the C functions and structures in a namespace:

namespace CFunctions {

#include "CHeader.h"

};

Now you refer to them as CFunctions::function() and such and it won't
clobber your global namespace with names you already have.
But then they will fail to link, assuming the C code is compiled with a
C compiler, all the symbols will be extern "C", not C++ mangled names.

Even if the C code is compiled with the C++ compiler, you will have to
conditionally include the namespace declaration for C++ compilation.

Better off just putting extern "C" round the inclusion of the header, or
better still conditionally included the wrapper in the header.
I misunderstood the problem. I figured he was dealing with names the
same as names he has.

Aug 22 '06 #5
Better off just putting extern "C" round the inclusion of the header,
Hello Ian, I apologize in advance if I misunderstood you, but are you
basically suggesting writing the following:

extern "C"
{
#include "CHeader.h"
}

or better still conditionally included the wrapper in the header.
???
Ian Collins wrote:
Noah Roberts wrote:
ruffiano wrote:
>A colleague of mine who is a C developer wrote several functions in C
which I now need to invoke in my C++ application. This is normally not
a problem except that the header file that he wrote contains also
several structures and #defines, which I need to use in my C++
application.

What is the best way to use the C functions, structures and defines in
my code without upsetting the compiler / linker?

For the C functions I know I need to use extern "C" { ...}. But how do
I get to include the structures and the defines? If I include the
header file in my C++ code, then the linker will fail.

You can embed the C functions and structures in a namespace:

namespace CFunctions {

#include "CHeader.h"

};

Now you refer to them as CFunctions::function() and such and it won't
clobber your global namespace with names you already have.
But then they will fail to link, assuming the C code is compiled with a
C compiler, all the symbols will be extern "C", not C++ mangled names.

Even if the C code is compiled with the C++ compiler, you will have to
conditionally include the namespace declaration for C++ compilation.

Better off just putting extern "C" round the inclusion of the header, or
better still conditionally included the wrapper in the header.

--
Ian Collins.
Aug 23 '06 #6
ruffiano,
refer this link,

http://developers.sun.com/prodtech/c...es/mixing.html

hope it will be helpful,

--
Raxit Sheth

Aug 23 '06 #7
ruffiano wrote:
>>Better off just putting extern "C" round the inclusion of the header,

Hello Ian, I apologize in advance if I misunderstood you, but are you
basically suggesting writing the following:

extern "C"
{
#include "CHeader.h"
}
Yes. Please reply inline, rather than top-posting.
>
>>or better still conditionally included the wrapper in the header.

???
in the header file, add

#ifdef __cplusplus
extern "C" {
#endif

before the definitions and

#ifdef __cplusplus
}
#endif

after them.

This is the usual way to make a header safe for inclusion in either C or
C++ code.

--
Ian Collins.
Aug 23 '06 #8
Ian Collins wrote:
>
Better off just putting extern "C" round the inclusion of the header,
Don't do that. It can cause horrible problems. The C header might pull
in other headers which, rightly, assume that they are not inside an
extern "C" block.

or
better still conditionally included the wrapper in the header.
Yup. Headers that will be used in both C and C++ need to conditionally
wrap their function prototypes in an extern "C" block:

#ifdef __cplusplus
extern "C" {
#endif

int my_funky_C_func(void);

#ifdef __cplusplus
}
#endif
Aug 23 '06 #9
In article <4l************@individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>ruffiano wrote:
>>>Better off just putting extern "C" round the inclusion of the header,

Hello Ian, I apologize in advance if I misunderstood you, but are you
basically suggesting writing the following:

extern "C"
{
#include "CHeader.h"
}
Yes.
IME, that forms should be avoided at almost all costs.
"funny things" start happening with typedef's, nested headers
and with compilers.

Only extern "C" what needs to be.

To quote Las Vegas: What happens in extern "C" stays in extern "C"
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 23 '06 #10
Greg Comeau wrote:
In article <4l************@individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>>ruffiano wrote:
>>>>Better off just putting extern "C" round the inclusion of the header,

Hello Ian, I apologize in advance if I misunderstood you, but are you
basically suggesting writing the following:

extern "C"
{
#include "CHeader.h"
}

Yes.


IME, that forms should be avoided at almost all costs.
"funny things" start happening with typedef's, nested headers
and with compilers.
And the standard explicitly prohibits including standard headers inside
any extern declaration, which in turn means that this isn't allowed if
CHeader.h uses any standard headers.
Aug 23 '06 #11
ra************@yahoo.co.in wrote:
ruffiano,
refer this link,

http://developers.sun.com/prodtech/c...es/mixing.html

hope it will be helpful,

--
Raxit Sheth
Thanks for this article, which specifically addresses the problem I'm
dealing with. I also appreciate all the other suggestions. In the end,
I edited the header file as follows:

#ifdef __cplusplus
extern "C" {
#endif
.... /* body of header */
#ifdef __cplusplus
} /* closing brace for extern "C" */
#endif

P.S.: Is this the correct way to reply inline?

Aug 23 '06 #12

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

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
25
by: John Hanley | last post by:
I have a program where both my main.c and program.c files use the program.h file. So I #include "program.h" in both the .c files. The program.h file has #ifndef PROGRAM_H #define PROGRAM_H...
18
by: Method Man | last post by:
If I don't care about the size of my executable or compile time, is there any reason why I wouldn't want to inline every function in my code to make the program run more efficient?
2
by: Mary | last post by:
Hello, I am having a problem with the cl compiler. I have written a C class (RegConnect.c) which uses Win32 API functions such as RegOpenKey, RegCloseKey etc. Initially when I was trying to...
1
by: CapMaster | last post by:
I've found some programs of how to create a standard game of blackjack on C++. But how would you do it using structs? Here is my assignment: Problem Statement: The purpose of this project is to...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
3
by: TamaThps | last post by:
I have to write a program that lets the user play the game of Craps. As of right now it is incomplete and I have run into some errors. It's my first time using functions instead of the program all...
38
by: abasili | last post by:
Hi everyone, I'm trying to compile a C++ function and then call it from a C program. Since Google is my friend I've ended up to this link which seems very clear: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.