60

I just made a .c file and compiled it with gcc in the terminal on OS X 10.8.2.

My syntax was gcc -o <filename> <sourcefile> and that was fine. Now I see I have an executable and file <filename> tells me as such, but I'm not sure how to actually run it beside double clicking the icon, /lame. When I try running the executable by just typing I get command not found, but I thought it was executable...?

Thank you.

EDIT: I"m so stupid, it was just open <filename>

2 Answers2

133

Unix will only run commands if they are available on the system path, as you can view by the $PATH variable

echo $PATH

Executables located in directories that are not on the path cannot be run unless you specify their full location. So in your case, assuming the executable is in the current directory you are working with, then you can execute it as such

./my-exec

Where my-exec is the name of your program.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • Thank you, but I thought that "." meant that the file would be hidden? My executable is not hidden. –  Feb 26 '13 at 03:53
  • 2
    Not sure where you got that idea. `.` refers to your current directory. – Perception Feb 26 '13 at 03:53
  • 1
    Here, http://en.wikipedia.org/wiki/Hidden_file_and_hidden_directory "In Unix-like operating systems a file or directory that starts with a period/full stop character (for example: /home/user/.config) is to be treated as hidden," –  Feb 26 '13 at 03:55
  • 7
    @algebr - The wiki you linked refers to files or directory that have a '.' as the first character in their name. What I am illustrating is the the second bullet point on [this page](http://edlab-www.cs.umass.edu/unixroot/node71.html). – Perception Feb 26 '13 at 03:57
17

To run an executable in mac

1). Move to the path of the file:

cd/PATH_OF_THE_FILE

2). Run the following command to set the file's executable bit using the chmod command:

chmod +x ./NAME_OF_THE_FILE

3). Run the following command to execute the file:

./NAME_OF_THE_FILE

Once you have run these commands, going ahead you just have to run command 3, while in the files path.

Anubhav
  • 1,984
  • 22
  • 17