Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've programmed my first program, typical one calculating the triangle square with Xcode and TextWrangler, and then I want to run it but I can't.

When I try to run it, Terminal says Command not found every time…

MacBook-Pro:~ Gaspar$ /Users/Gaspar/Desktop/prova.c 
-bash: /Users/Gaspar/Desktop/prova.c: Permission denied
MacBook-Pro:~ Gaspar$ sudo /Users/Gaspar/Desktop/prova.c 
sudo: /Users/Gaspar/Desktop/prova.c: command not found

How could I configure it so I don't have to enter the directory all the time?

share|improve this question
    
Have you compiled the program? –  Lucas Kauffman Sep 17 '12 at 10:44
    
@LucasKauffman yes, with gcc and Xcode. –  gasbi Sep 17 '12 at 10:55
2  
You need to run the compiled binary executable (probably prova), not your source code (certainly not prova.c, which is just plain text and not a script/binary). –  Breakthrough Sep 17 '12 at 10:58

2 Answers 2

gcc /Users/Gaspar/Desktop/prova.c -o Users/Gaspar/Desktop/prova
/Users/Gaspar/Desktop/prova

make sure it have executable flag set (it should have by default) if not set it:

ls -l Users/Gaspar/Desktop/prova

chmod +x Users/Gaspar/Desktop/prova
share|improve this answer

The file prova.c is a source file. It's plain-text and contains your program's source code. Once you've compiled it, a new file will be created, probably prova (with no extension). This is the executable – a binary file – which you can run from your terminal.

Many executables in your shell are available everywhere, without having to type the full path. For example, sudo, is actually /usr/bin/sudo, but your shell will know to search in /usr/bin. However, your own programs are not automatically found by the shell. You'll have to tell it where it is.

Simply type:

/Users/Gaspar/Desktop/prova

If you don't want to write the full path, just change your working directory before:

cd /Users/Gaspar/Desktop/

Now you can call your program like this, where . specifies the current directory. You'll have to include the ./ since your current directory normally is not in the list of paths where your shell searches for executable programs.

./prova

This might not work if the binary does not have execute rights set. This is a flag that allows the file to be run from a terminal. If you get a permission denied error, add the execute permission:

chmod +x prova

Now, try running the file again with ./prova.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.