Mixing C inside MFC programs
-
I'm not much of an MFC/C++ guy but I know C. I'd seen in some forum here, that we can mix MFC with Console apps. I dunno whether he meant console app to be C++ or not! My qstn is how to use C in an MFC [exe] project. What extra things should I do to mix C inside so that I too can do some MFC stuff and porting some of my progs to GUI? Thanks
-
I'm not much of an MFC/C++ guy but I know C. I'd seen in some forum here, that we can mix MFC with Console apps. I dunno whether he meant console app to be C++ or not! My qstn is how to use C in an MFC [exe] project. What extra things should I do to mix C inside so that I too can do some MFC stuff and porting some of my progs to GUI? Thanks
blacksalt wrote:
My qstn is how to use C in an MFC [exe] project
It depends what do you mean by "C". If you simply have source files (with C code) that you want to add to your project, then you can simply add them to the project and call the functions. Basically C++ is just an improvement of C.
Cédric Moonen Software developer
Charting control -
I'm not much of an MFC/C++ guy but I know C. I'd seen in some forum here, that we can mix MFC with Console apps. I dunno whether he meant console app to be C++ or not! My qstn is how to use C in an MFC [exe] project. What extra things should I do to mix C inside so that I too can do some MFC stuff and porting some of my progs to GUI? Thanks
1. Include any external C source code files in the project, the same as including a C++ file. The compiler knows the difference between the two file types base on the file extension. 2. Enclose the prototypes for the C functions in the header file or anywhere that a C prototype is provided [in a C++ file], using the extern “C” declaration as follows:
extern "C"
{
void prn();
}3. Now you are set to do what ever you want as far as MFC development is concerned. There is one other possibility; copying all your C files along with their headers into the project folder and changing the extension to ‘.cpp’. I generally do not recommend this if you have other C programs that depend on the same sources, as it is usually better to avoid having multiple copies of the same sources. Then again doing this makes the sources more specific to the C++ project being developed. Any way you get the general idea of how it works. Oh, you will have to change any code that is dependent on user interaction via standard input/output or any other non-Windows method. The difficulty of that depends on how you wrote the original code to begin with. Good Luck! INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
1. Include any external C source code files in the project, the same as including a C++ file. The compiler knows the difference between the two file types base on the file extension. 2. Enclose the prototypes for the C functions in the header file or anywhere that a C prototype is provided [in a C++ file], using the extern “C” declaration as follows:
extern "C"
{
void prn();
}3. Now you are set to do what ever you want as far as MFC development is concerned. There is one other possibility; copying all your C files along with their headers into the project folder and changing the extension to ‘.cpp’. I generally do not recommend this if you have other C programs that depend on the same sources, as it is usually better to avoid having multiple copies of the same sources. Then again doing this makes the sources more specific to the C++ project being developed. Any way you get the general idea of how it works. Oh, you will have to change any code that is dependent on user interaction via standard input/output or any other non-Windows method. The difficulty of that depends on how you wrote the original code to begin with. Good Luck! INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
OK I tried adding all C and .h files to the MFC project. There are many errors. I'll change all the printf() calls to Afxmssg type. Now one of the main culprit is directory fns & variables. I think those also need to be replaced with MFC part. With code properly arranged out I'll get back to you. As of now there are no calls from MFC code to any of the C fns. I understand that this has to be done within externC. Thanx for the reply.
-
OK I tried adding all C and .h files to the MFC project. There are many errors. I'll change all the printf() calls to Afxmssg type. Now one of the main culprit is directory fns & variables. I think those also need to be replaced with MFC part. With code properly arranged out I'll get back to you. As of now there are no calls from MFC code to any of the C fns. I understand that this has to be done within externC. Thanx for the reply.
This is the first time I have been back since I posted my answer to you, but it appears you get the idea. Please do not use synonyms or Web acronyms when talking about coding, use the complete word so every one knows what you are talking about (assuming fns means function). During the process I would be surprised if you did not receive many errors. Most of those should be related to I/O, always try to separate the I/O from the code that actually does the work as much as possible. Your ultimate goal is to write a solution (program) that has a back end (which does the work) and a front end (which show it to the user). If you do it right, then the back end will be portable (at least in theory). INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra