File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Linux / UNIX and the fly likes gcc on linux Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Reply Bookmark "gcc on linux" Watch "gcc on linux" New topic
Author

gcc on linux

Parag Mokal
Greenhorn

Joined: Jan 14, 2001
Posts: 19
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
you need to put ./ before a.out so it knows to look in your current directory for the executable file...
$ ./a.out


what?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14480
    
    7

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
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
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
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
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
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: gcc on linux
 
Similar Threads
Runtime.getRuntime() help plzzzzzzzz
loss of precision error
c++
How to call a Linux program from Java?
Compile shared lib (g++)