Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Mixing C inside MFC programs

Mixing C inside MFC programs

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    blacksalt
    wrote on last edited by
    #1

    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

    C J 2 Replies Last reply
    0
    • B blacksalt

      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

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • B blacksalt

        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

        J Offline
        J Offline
        John R Shaw
        wrote on last edited by
        #3

        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

        B 1 Reply Last reply
        0
        • J John R Shaw

          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

          B Offline
          B Offline
          blacksalt
          wrote on last edited by
          #4

          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.

          J 1 Reply Last reply
          0
          • B blacksalt

            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.

            J Offline
            J Offline
            John R Shaw
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups