c newbie question

Discussion in 'Mac Programming' started by jamesapp, May 21, 2008.

  1. jamesapp macrumors 6502a

    Joined:
    Mar 7, 2008
    #1
    just wondering about compiling c programs
    i typed in and ran a program which i called hello.c
    the program compiled and ran successfully, i saved this program to a folder on my computer. it created an executable file called a.out. when i tried to compile another program which i called fahr.c it compiled, but when i listed the contents of my folder there were only 3 files. a.out, fahr.c and hello.c.
    when i ran a.out it ran the file which i had last compiled fahr.c.
    just wondering what i should do if i want to have multiple files in the same directory?
    one thing i will add is that when i compiled my second program like this

    Code:
    gcc fahr.c -o a1.out
    
    it created an executable file called a1.out
    and when i listed the contents of the folder where i am storing my files,
    there were four files in the folder.
    just wondering if this is how i should be compiling my programs?
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Yes, use the -o option to set names. But don't call them a1.out a2.out or whatever, call them something reasonable and rememberable.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    It's normally easiest to call your binary the same thing as your source file that contains int main. That makes it much easier to identify. So for example:
    gcc -o hello hello.c
    ./hello

    gcc -o fahr fahr.c
    ./fahr

    It's not strictly necessary, but you may want the output to go into a separate bin folder to keep your source and output a bit tidier, but this certainly isn't necessary.

    -Lee
     

Share This Page