Porting C code (with GUI) created in VisualStudio to Mac with least amount of effort?

Discussion in 'Mac Programming' started by illjazz, Jan 30, 2011.

  1. illjazz macrumors regular

    Joined:
    Apr 18, 2008
    #1
    I have a few C programs with GUIs that I'd like to continue using and working on when I get my new Mac. The code and GUI were written in VisualStudio. Is there anything comparable to VisualStudio on the Mac that will let me run my already existing code with minimal effort? Would Xcode be suitable for this?

    Thanks!
     
  2. kainjow, Jan 30, 2011
    Last edited: Jan 30, 2011

    kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    So Win32/MFC? That is not portable. You're going to have to rewrite all your GUI code in Cocoa.

    Yes, Xcode is what you'll want to use.

    Edit: looking at your other thread it appears you're taking a programming class. Most likely you're learning MFC which is Microsoft's framework for helping write GUIs under Windows. This code won't run/compile on anything but Windows. In order to write an equivalent GUI on the Mac you'd have to use a different library called Cocoa, which is probably beyond the scope of what you're doing.
     
  3. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #3
    IMHO this is one of the great things about the MVC model used in Mac development. It tends to separate out the UI from the worker (model) code along relatively simple lines.

    I "inherited" an internal MFC app that was unstable when I got it, but I didn't have enough C++ experience at the time to try and stabilize it myself. So I found a willing "victim" and tasked him with the job. His approach was to separate the UI from the working bits of the program and at the same time build a simple unix compatible CLI. That version compiles fine on Linux and Mac OS X.

    Unfortunately I handed the project off to another engineer and it went back to its old ways. Stuff that should have been in its own model classes ended up mixed in the UI, and instability crept back in. So when I got control of it back I just forked the code back at the earlier code in order to add new features.

    Moral of the story. Switching OSes and UIs is greatly facilitated if you structure your code appropriately.

    Either that, or just look into Mono if you are using managed code. :p

    B
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    It seems that structuring your code appropriately is also greatly facilitated by being forced to care for OS and UI portability :D
     
  5. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    :D Yeah, but in this case that was a happy by-product and not the end goal.

    We only got a unix executable out of it because the guy I hired was a Linux guy and wanted to do as much of the work as possible within Linux. And my later frustration when I got the code back after some years was that I needed some code for a different Windows project, but it was buried so deep in two different pieces of UI code that I couldn't easily extract it.

    Structuring your code as model and UI also greatly facilitates debugging and code re-use since you don't have to carry your UI around just to figure out what your model code is doing and you can also test out your UI with fake model data.

    B
     
  6. Winni macrumors 68030

    Winni

    Joined:
    Oct 15, 2008
    Location:
    Germany.
    #6
    I think you're in a world of hurt. You might want to look into Qt and port your code to that multi-platform framework. The guys who are porting the great Mac application Scrivener to Windows (and Linux) are using it and the result looks great.

    Another option would be to port your code to C# and use Mono and its (commercial) frameworks. But since you have existing C code, Qt will probably be the better way to go.
     
  7. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    Of course there are other approaches. Like just running Windows and Visual Studio in a VM, and using winebottler to treat the EXE as an .app.

    B
     
  8. Bill McEnaney macrumors 6502

    Joined:
    Apr 29, 2010
    #8
    But if you're like me, you would rather avoid the overhead that comes with a VM. Besides, i've heard that, although a virus for Windows platform won't harm Mac hardware, that virus can attack a VM you're using to run a Windows program. Since Macs are new to me, I don't know whether I'm pointing out a major problem. That's why I hope some experts here will tell us about potential dangers if there are any when you run a VM on a Mac.
     
  9. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #9
    Generally speaking the worst a Windows virus can do in a VM is probably corrupt some files in the VM's virtual disk. It's not going to do anything to OS X or the hardware. That's the beauty of virtual machines: you can keep a backup copy of the whole system in a happy state, and revert back to it if something goes wrong with your active copy.
     
  10. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #10
    One huge advantage of a VM is that you can snapshot a clean install. Just keep a copy of your source code files onto another virtual drive (or SVN repository). Let the Windows VM catch the flu or whatever, halt it, blow it away, scan to make sure your source code drive or repository includes no binary or executables, then restart a clean machine from the earlier snapshot.
     
  11. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #11
    And depending on your level of paranoia, you can even have your VM have no network access to further reduce the likelihood of "infection"

    +1 for integrating source control management/version control in your workflow early. Even if you are the only contributor to the code, I find it invaluable to be able to go back in time on a granular level see the changes I made and why. If you don't want to learn how to set it up yourself, use a service like Beanstalk.

    B
     
  12. illjazz thread starter macrumors regular

    Joined:
    Apr 18, 2008
    #12
    Thanks for the helpful responses, everyone! I asked this question for a friend of mine and not for myself, so I pointed him to the thread and he found it very helpful.

    I recommended Parallels in case he wanted to go the VM route, and it sounds like that's what he'll end up doing for the time being.

    Thanks again.
     

Share This Page