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
Joined: Apr 12, 2001
Posts: 1012
posted
0
you need to put ./ before a.out so it knows to look in your current directory for the executable file... $ ./a.out
This is because unlike DOS, the current directory is not implicitly included in the PATH.
Customer surveys are for companies who didn't pay proper attention to begin with.
jasonkosowan
Greenhorn
Joined: Sep 07, 2001
Posts: 25
posted
0
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
Joined: Sep 26, 2000
Posts: 919
posted
0
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
Joined: Oct 10, 2001
Posts: 3
posted
0
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
Joined: Sep 26, 2000
Posts: 919
posted
0
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
Joined: Oct 10, 2001
Posts: 3
posted
0
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
Joined: Oct 10, 2001
Posts: 3
posted
0
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.