Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have lot of mathematical functions implemented in C language. these file name extension are .c. now I want to build MFC app in visual C++.

So how shall I reuse these legacy C code in my C++ code? I came up with some ideas, such as change file name to .cpp and then add them into my MFC projects.

but I try to find advice from you experts and gurus...
Thank a million!

What I have tried:

tried some simple case and my idea is working...
Posted
Updated 10-Oct-20 22:25pm
v2

That's one way ... and since C++ is C with add-ons, it should work fine (and ensure that the code is built for the same since: x86 vs 64 bit for example.

The other way is to build it as a library and link that into your app so you don't need to make any real changes to it.

I prefer libraries myself as it's harder to muck up the code by mistake ... :laugh:
 
Share this answer
 
Comments
Southmountain 10-Oct-20 17:19pm    
thanks OG! because I have source code, I am thinking to add them into project directly...
OriginalGriff 10-Oct-20 17:34pm    
Seriously, I'd build a library file and include that in the project: it makes source control so much easier!
Southmountain 10-Oct-20 18:09pm    
did you put lib into your code repository?
OriginalGriff 11-Oct-20 3:38am    
Do you want to try asking that in English? :laugh:
CPallini 13-Oct-20 2:00am    
5.
Make a header file with the prototypes for all of your functions. Wrap them in an extern statement like this :
C++
extern "C" {

// prototypes go here

}

{ edit }

If you expect your file to be used in both C and C++ then this is the customary way to handle that :
C++
#ifdef __cplusplus
extern "C" {
#endif


// prototypes for things that need to be labelled "from C" go here


#ifdef __cplusplus
}
#endif
 
Share this answer
 
v2
Comments
Southmountain 11-Oct-20 23:57pm    
thank you to share this! when I implement this function prototype, do I need to put this extern statement again?
extern "C"{
//implementationint function_name(int,int)
{  
//code logic here
}
}
Rick York 12-Oct-20 0:45am    
When you implement the functions they will be in a .c file right? You don't need another extern statement if so. If you put them in a .cpp file or use the c++ compiler for the file then you will need the extern statement again.
Southmountain 12-Oct-20 12:52pm    
yes, I prefer to have my .c files staying intact. my understanding is assured by your confirmation!
thank you a million!
Rick York 12-Oct-20 19:38pm    
One last thing, if your file will be used in c++ AND c then it is usually handled with another layer of macro. Please see the revised solution.


Southmountain 13-Oct-20 14:11pm    
thank you very much!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900