PDA

View Full Version : Compiling code written in Ubuntu




Schraiber
Aug 4, 2009, 11:30 AM
Hey all,

I have some C code that I wrote on an Ubuntu machine, which I figured I could just directly port over... it's pretty simple; I'm mostly trying to recompile for laughs, since I'm not a serious programmer.

Nonetheless, when I try to compile the unaltered code from the command line with gcc, it creates an a.out file but if I try to run it it just says "command not found". I assume that there's something I gotta do to port the code, but I don't know what it is...

*feels kinda stupid*



mkrishnan
Aug 4, 2009, 11:34 AM
It sounds like you're missing (possibly among other things) something very basic... you're referencing the a.out file by path, right? (i.e. ./a.out) Maybe post a terminal capture.

iShater
Aug 4, 2009, 11:39 AM
Don't you need to link the a.out file to libraries to generate an exe? :confused:

Edit: I'm probably thinking something totally off. Never mind me. :p

Cromulent
Aug 4, 2009, 11:55 AM
The command you are looking for is:

./a.out

mslide
Aug 4, 2009, 12:06 PM
Nonetheless, when I try to compile the unaltered code from the command line with gcc, it creates an a.out file but if I try to run it it just says "command not found". I assume that there's something I gotta do to port the code, but I don't know what it is...

Just to further explain here. The a.out file created is the program executable. a.out is the default executable name when you don't supply one to gcc. Why you got command not found when you tried to run it was already explained. If you want to name your program executable something meaningful, then add a "-o <program_name>" to your gcc command line.

Another way to compile it is to use make. If it's a very basic program that is all contained in a single source file, doesn't need any special compile flags set, doesn't need any include paths passed to the compiler etc, then the following will work even if you didn't create any makefiles:

If your source file was named 'foo.c' then ...

make foo

This will create an executable called 'foo'. Run it by doing:

./foo

Schraiber
Aug 4, 2009, 12:14 PM
Thank you guys so much! For some reason, in the Ubuntu shell all I needed to type was "a.out" and it would work.

mkrishnan
Aug 4, 2009, 12:16 PM
Thank you guys so much! For some reason, in the Ubuntu shell all I needed to type was "a.out" and it would work.

That's actually weird... were you not using bash in linux?

Anyway, glad you figured it out.

lee1210
Aug 4, 2009, 12:22 PM
That's actually weird... were you not using bash in linux?

Anyway, glad you figured it out.

Bash doesn't stop you from adding . to your path. One could do so on OS X, but it's a bad idea everywhere.

-Lee

jpyc7
Aug 4, 2009, 12:25 PM
Thank you guys so much! For some reason, in the Ubuntu shell all I needed to type was "a.out" and it would work.

That just means your current directory (on Ubuntu) was in your PATH, which negated the requirement of the dot and slash characters. It can sometimes be considered a security risk depending on ordering of directories in your PATH environment variable (because a downloaded executable file could take precedence over a system-installed file with the same name).

mkrishnan
Aug 4, 2009, 12:53 PM
Bash doesn't stop you from adding . to your path. One could do so on OS X, but it's a bad idea everywhere.

Yeah, sorry, I was assuming that no one would do this. :o

Cinder6
Aug 4, 2009, 01:38 PM
Yeah, sorry, I was assuming that no one would do this. :o

My school shell account does it. It's annoying, because people get used to it and then get really confused if they use another *nix machine. UC Davis does the same thing, actually. It's dumb :)