Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
Okay

I have written two simple programs, helloworld.c and merrychristmas.c

Both compiled and executed okay, but my question is that in my documents folder I have both programs, but one a.out.

So when I first did helloworld, I typed ./a.out then my program executed and said hello world.

Then I compiled my second program (merrychristmas) and I typed ./a.out and it executed.

My question/dilemma is when I type ./a.out, it seems to be executing my most recent program. if I recompile, I guess that's what I'm doing when I type gcc ~/Documents/merrychristmas.c then it will execute the program I'm recompiling.

Do I need to re-compile as I showed in the above example every time to run the program I choose?

Or is there another way to run the program within terminal, without entering gcc again?

Thanks, and Merry Christmas to everybody out there!!
 
The a.out in the directory will always be the the program that you last compiled. So if you compiled helloworld.c last then that's what it will be. If you want to be able to run both of them without recompiling then you have a couple of options. You can either rename the a.out file after recompiling (mv a.out helloworld) or you can compile it with the output option (gcc helloworld.c -o helloworld).
 
You can either rename the a.out file after recompiling (mv a.out helloworld) or you can compile it with the output option (gcc helloworld.c -o helloworld).

In either case don't forget to run the program as ./helloworld or ./merrychristmas after using -o or renaming a.out. ;)

B
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.