Compiling code written in Ubuntu

Discussion in 'Mac Programming' started by Schraiber, Aug 4, 2009.

  1. Schraiber macrumors member

    Joined:
    Jun 25, 2009
    #1
    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*
     
  2. mkrishnan Moderator emeritus

    mkrishnan

    Joined:
    Jan 9, 2004
    Location:
    Grand Rapids, MI, USA
    #2
    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.
     
  3. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #3
    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
     
  4. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    The command you are looking for is:

    Code:
    ./a.out
     
  5. mslide macrumors 6502a

    Joined:
    Sep 17, 2007
    #5
    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 ...

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

    Code:
    ./foo
     
  6. Schraiber thread starter macrumors member

    Joined:
    Jun 25, 2009
    #6
    Thank you guys so much! For some reason, in the Ubuntu shell all I needed to type was "a.out" and it would work.
     
  7. mkrishnan Moderator emeritus

    mkrishnan

    Joined:
    Jan 9, 2004
    Location:
    Grand Rapids, MI, USA
    #7
    That's actually weird... were you not using bash in linux?

    Anyway, glad you figured it out.
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    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
     
  9. jpyc7 macrumors 6502

    Joined:
    Mar 8, 2009
    Location:
    Denver, CO
    #9
    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).
     
  10. mkrishnan Moderator emeritus

    mkrishnan

    Joined:
    Jan 9, 2004
    Location:
    Grand Rapids, MI, USA
    #10
    Yeah, sorry, I was assuming that no one would do this. :eek:
     
  11. Cinder6 macrumors 6502

    Cinder6

    Joined:
    Jul 9, 2009
    #11
    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 :)
     

Share This Page