473,471 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

mixing c and c++ code in one project

11 New Member
I have a c++ project and i have a c code I need to mix them both in one solution .....I still get errors .
I need the c code to be in one class and I need to call it from a c++ main()/
any official technique?!
May 22 '11 #1
2 2514
weaknessforcats
9,208 Recognized Expert Moderator Expert
You can mix C and C++ coded but there's a trick to it.

First, C++ has function overloading. That is, you can have muiltiple functions of the same name as long as the arguments are different.

Second, C++ uses a decorator, or mangler, to hash a functions and it's arguiments to a unique symbol. This symbol, which may look like MHG_FYYZ is tghe name of the function called by C++ functions.

Third, a C function is not mangled. So when you call a C++ function from C you use the C++ function name and not the mangled name. Since these two name are never the same you can never call the C++ function using it's C name.

Fourth, The trick: Tell the C++ compiler that the C++ function will be called from C and ot please turn off the name mangler. You lose function overloading in C++ for this function since all function names in C++ must be unique. That's why the mangler in the first place.

To do this you use extern "C" in the C++ code:

Expand|Select|Wrap|Line Numbers
  1. extern "C" void MyFunction(int);
in the header file and:
Expand|Select|Wrap|Line Numbers
  1. extern "C" void MyFunction(int)
  2. {
  3.    //function body
  4. }
Note that
Expand|Select|Wrap|Line Numbers
  1. extern "C"
appears only in the C++ code.

Fifth, Therefore any C function called by C++ must be extern "C" in C++. Likewise, any C function calling C++ functions must be extern "C" in C++.

No changes are ever made to the C code.
May 22 '11 #2
mac11
256 Contributor
As a follow up to weaknessforcats, there's a C++ faq section for mixing C and C++
http://www.parashift.com/c++-faq-lit...c-and-cpp.html
May 23 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Loyd Christmas | last post by:
As a non-techie, I am curious if my developers can mix VB.Net and C# in the same project and be OK. Seems to be some disagreement here.
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
12
by: Joe via DotNetMonster.com | last post by:
Hi, Within the HTML if I try to put an if statement, I get an error: BC30201: Expression expected This is a simplified example: <%# If StoreNumber = "1" Then %> <b>Store1</b>
29
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form...
1
by: Howie | last post by:
Hi, is there a way to crypt a softwareproject (c++) to confirm only two persons together can compile these project ? Any suggestions ? Thanks in advance, Howie
0
by: IlQlo | last post by:
Hi All. I'm looking for a control to use in VB.NET 2005 that merge a Listview functions with a Treeview. A good example is this one: http://www.codeproject.com/cs/miscctrl/treelistview.asp ...
28
by: ziman137 | last post by:
Hello all, I have a question and am seeking for some advice. I am currently working to implement an algorithmic library. Because the performance is the most important factor in later...
4
by: Janne Pyykkö | last post by:
Hello, Is it ok to let users download DShowNet.dll from my site instead of www.codeproject.com that needs username & password? The software using dll is for detecting meteors from night sky and...
2
by: Jon Slaughter | last post by:
How difficult is it for one to integrate unmanaged C++ into C#? I know for functions one can use DLLimport but how does one go about doing it for classes? Do I have to completely reimplement the...
2
by: rhino | last post by:
Can you have muliple DB2 instances that are at different code levels on the same computer? For example, can I have an instance of DB2 V8 and an instance of DB2 V9 on the same Windows machine? I...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.