473,785 Members | 2,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2583

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::fun ction() 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::fun ction() 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::fun ction() 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::fun ction() 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******@hotma il.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

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

Similar topics

5
3348
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 charcters in it, that the code needs to run until all bad characters are gone? For example, if a file has the name "<bad*mac\file" the program has to run 3 times to get all three bad chars out of the file name. The passes look like this:
6
3502
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 written. /* Book name : File name : E:\programs\cpp\iti01\ch10\ex09_5p1.cpp Program discription: Adding name,Address to customer_record SETUP PROGRAM
25
4920
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 .... #endif Yet when I build my program, the DJGPP compiler tells me there are multiple definitions of each of my functions.
18
5069
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
5840
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 create a dll, using the cl compiler I was getting many unresolved external errors, Example 1 below (8 in total, 7 to do with the registry function calls and 1 from wsprintfA call) as I hadn't included the AdvAPI32.lib file. So I created a LINK...
1
5662
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 create a game of Blackjack, which can be played by one player against the dealer (represented by the computer). The deck of cards is to be stored as an array of Card structures. Blackjack is played with a deck of 52 cards, consisting of four...
21
34439
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 obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
221
367740
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 needs to store entire files, the preferred method is to save the file onto the server’s file-system, and store the physical location of the file in your database. This is generally considered to be the easiest and fastest way to store files. ...
3
2901
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 being in the int main(). I am using visual studio on windows XP. In my code I've defined a local variable in a function but when I run the program and it gets to that function it says I have an undeclared variable. The code for the program...
38
2667
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: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html Unfortunately it does not work. Here is what I'm doing: -----------------------------------------
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.