beginner help in C compiling

Discussion in 'Mac Programming' started by uaecasher, Jun 12, 2009.

  1. macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #1
    hello,

    I was writing a simple hello world program in C, i have compiling the program in terminal using the code

    $ gcc project.c

    i got the file a.out

    now i want to run it i use the code

    $ a.out

    but i get this error:

    -bash: a.out: command not found


    here is the source code for hello world program:

    Code:
    
    #include <stdio.h> 
    int main (void) 
    { 
    	printf ("Programming is fun.\n"); 
    	return 0; 
    } 
    
    
    
     
  2. macrumors 6502

    Joined:
    Jun 2, 2009
    #2
    Try this:

    Code:
    ./a.out
    
    The current directory normally isn't in the path for security reasons. Imagine if someone created a virus and named the executable "ls".
     
  3. thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #3
    it worked but i didn't understand what was wrong

    thanks
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    You gave no full path for the file you wanted to run, so the system searched through the directories in your PATH environment variable. The current directory, referred to as ., is not in PATH by default for security reasons. This means that if you want to run a program that is in your current directory, you need to give an explicit path.
    ./a.out
    Is a path to the file a.out in the current directory.

    -Lee
     
  5. macrumors 6502

    Joined:
    Apr 24, 2008
    #5
    To make "security reasons" a bit more concrete, the idea is that if the current directory would be in the path, there could be malicious programs in certain directories which would have the name of a popular unix command, but misspelled (for instance, "mroe" instead of "more"). I you happened to be in that directory and you'd type in "mroe", instead of getting "-bash: mroe: command not found", the malicious program would run.

    Even worse would be to have the current working directory as one of the first directories in the path, since even correctly spelled unix commands could be "hijacked" that way.
     
  6. macrumors 6502

    Joined:
    Jun 2, 2009
    #6
    On UNIX, every command is a file somewhere. This is different on DOS/Windows, where some commands (like DIR) are built in to the shell. When you type a command on UNIX, the system has to know where to look for the file. Try typing this in the terminal:

    Code:
    echo $PATH
    
    You will get a list of directories where the system will look if you type a command without a full path.

    Code:
    /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
    
    For example, when you type "ls", it will try "/usr/bin/ls" and find that that file does not exist. Then it will try "/bin/ls" and find that that file does exist, so it will run it.

    The current directory is always represented by the dot. Since there isn't a "." in the $PATH variable, the system doesn't know where to find your file. The reason that there is not "." in the $PATH is that you always want to know exactly what command you are running. Consider what would happen if you set your path to this:

    Code:
    .:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
    
    Now, since the first directory in the path is ".", you could just type "a.out" to run your program. However, I could create a program that erases your entire hard drive, and call the file "ls". Then if you happened to be in the directory where I put that file and tried to get a directory listing, my program would run instead of the system "ls" that you were expecting.

    (I just noticed that a couple others replied as I was typing this, so most of this has been said already in one way or another.)
     
  7. thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK

Share This Page