How to Compile a c or c++ file to run on Windows?

Discussion in 'Mac Programming' started by cathy88, Nov 14, 2009.

  1. cathy88 macrumors member

    Joined:
    Aug 11, 2009
    #1
    Hey, Ive been doing some c language at school but it was to do with microprocessors, anyway Im wondering if its possible to compile my code into .exe excutable so Windows command window could be used. I know C++ can I think from memory but Im really unsure could someone point me into a direction on how or what to read? Below is my code

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int main(void)
    {
    int minNum=0;
    int maxNum=100;
    srand ( time(NULL) );
    int num=rand()%100;
    int count=0;
    int guessNum;
    printf("Number Guessing Game\n\n");
    printf("Guess a number between 0 to 100\n");
    while(1)
    {
    scanf("%d",&guessNum);
    if (guessNum != num && guessNum > num && guessNum<=100) {
    printf("The number is between %d to %d\n",minNum,guessNum);
    maxNum=guessNum;
    count++;
    }
    else if(guessNum != num && guessNum < num) {
    printf("The number is between %d to %d\n ",guessNum,maxNum);
    minNum=guessNum;
    count++;
    }
    else if(guessNum >100){
    printf("Please enter a value within the range %d is not within the range of 0-100\n",guessNum);
    }
    else if(guessNum==num){
    count++;
    break;
    }
    }
    if (count==1) {
    printf("YOU WON! the number is %d took you %d guess\n",num,count);
    }
    else{
    printf("YOU WON! the number is %d took you %d guesses\n",num,count);
    }
    return 0;
    }
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Sure just install Mingw on Windows and then compile it there.

    There maybe a solution for Mac OS X but if there is I have never heard of it. I imagine it is far easier to install Windows in VirtualBox and then compile it there than it is to get a working tool chain that produces Windows executables.

    Although having said that if you only use the standard library it might work if you get GCC to spit out ASM and then use NASM to assemble it as a Windows executable. I know if you compile NASM yourself it supports most binary formats.

    Edit: I'm not sure if GCC can output Intel style ASM or even if it is in a NASM compatible syntax.
     
  3. cathy88 thread starter macrumors member

    Joined:
    Aug 11, 2009
    #3
    Hey thank you for the help, Im just programing for fun starting with something very small ( a stupid guessing game:D). Anyway mind explaining what NASM or ASM means and or how it could be used in command window?
    Meanwhile Ill try the program suggested for Windows.

    Thanks

    edit:

    Im at the site there is all these files Im not quite sure which I need and which I dont need, would you mind giving a hand on which I should download?
    http://sourceforge.net/projects/mingw/files/

    Thanks again
     
  4. abbaskywalker macrumors newbie

    Joined:
    Jun 9, 2009
    #4
    hi, i compiled it on my mac, i couldnt get whats the exact problem you have?
    i am using xcode tools.
     
  5. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    She wants to compile it for Windows not Mac OS X. That is the issue.

    Sure. Download the Installer from here. Get version 5.1.6.
     
  6. cathy88 thread starter macrumors member

    Joined:
    Aug 11, 2009
    #6
    Ive donwloaded and installed it but was unsure of what to do, then I also installed cygwin and c compiler and compiled the file on windows, it came out as .exe but when I click it it wont open gives an error, what am I suppose to do?

    Thanks
     
  7. m3kilpat macrumors regular

    Joined:
    Jul 6, 2009
    #7
    If you want to make a .exe file that you can simply click to run you need to use Microsoft Visual Studio. The executable file created by gcc or g++ with cygwin creates a unix executable. If you want to run what you compiled with gcc or g++, go to the directory where the file is located (do this in cygwin) and the type "./program" without the quotes where program is your program name.
     
  8. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #8
    Just to make sure we're understanding you correctly: you want to compile a program on your Mac which can be run on a Windows system?
     
  9. mslide macrumors 6502a

    Joined:
    Sep 17, 2007
    #9
    You don't need to use Visual Studio to make a program that can be 'double-clicked' on. A program compiled in the cygwin environment (with cygwin gcc) will run outside of the cygwin environment and by just double clicking on it from within windows explorer. That's what the cygwin1.dll file is for ... to make it run. You can also run the program on a windows computer that does not have cygwin installed as all as long as you distribute the cygwin1.dll file along with it. Same goes for things compiled with mingw/msys except you need to distribute the mingw dll file along with.
     
  10. m3kilpat macrumors regular

    Joined:
    Jul 6, 2009
    #10
    Right. I meant to create a .exe file that will execute on any Windows machine without other files needed.
     

Share This Page