• Post Reply Bookmark Topic Watch Topic
  • New Topic

gcc on linux  RSS feed

 
Parag Mokal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,
I've linux installed on my machine. But when I compile a C program with 'gcc' command, the program gets compiled and an 'a.out' executable is created. But when I try to run this executable, it gives me a bad command.
Can anyone tell me how to compile and run C programs on linux?
 
Greg Harris
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to put ./ before a.out so it knows to look in your current directory for the executable file...
$ ./a.out
 
Tim Holloway
Saloon Keeper
Posts: 18792
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because unlike DOS, the current directory is not implicitly included in the PATH.
 
jasonkosowan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try doing this, assuming you are running bash: First set your PATH up correctly
export PATH=.:$PATH
Now, when you compile, make sure to use the -o option on gcc so you don't mistakenly clobber some other executable. You shouldn't really be creating an a.out to begin with:
gcc ./myprog.c -o ./myprog
This should get you started
Originally posted by Parag Mokal:
Hello everybody,
I've linux installed on my machine. But when I compile a C program with 'gcc' command, the program gets compiled and an 'a.out' executable is created. But when I try to run this executable, it gives me a bad command.
Can anyone tell me how to compile and run C programs on linux?

 
George Brown
Ranch Hand
Posts: 919
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jasonkosowan:
Try doing this, assuming you are running bash: First set your PATH up correctly
export PATH=.:$PATH

in this case there is no 'correct' or 'incorrect'. Just different levels of risk. I humbly suggest that setting up your path with the current directory at the end of the colon-separated list is much less problem-prone, if you must put the current directory on your path at all:
export PATH=$PATH:.
There are good reasons why the current directory is not included on your path by default under unix/linux/bsd etc.
 
zx11
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys (and Gals):
I have the same type problem except I still get a "Command not found" even when I add the "./" before my cleanly compiled and linked C code. Apparently my executable is "not seen" as executable by Red Hat Linux. Any ideas?
thanks
 
George Brown
Ranch Hand
Posts: 919
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using the 'chmod' command to make your process executable. Assuming your binary is called 'foo' and is in the current directory, type:
chmod +x foo
You might also want to check up on the 'chmod' command in the manpages because there are alternative and more accurate ways of setting executable (and other) permissions that you should get to know.
HTH.
 
zx11
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks George. I tried it (actually set to rwxrwxrwx) and still get the "Command not found" problem. I wonder if my Makefile needs something (gcc option ?) to say that I am making an executable binary.
Thanks for your help.
 
zx11
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've figured out the problem, and it is interesting. I did a command-line compile/link, and the executable now works. I think the make utility is faulty.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!