Can someone please tell me how to compile from the command line in a clear, concise way. Preferably, my programs will be written in a real text editor (such as leaf pad).


I am a complete n00b when it comes to Linux, C, and the Raspberry Pi. So, the three have come together in a perfect storm to attack!

I have created a C file called main.c in a folder. The directory is:

/home/pi/Desktop/Data Base/main.c

The C code is a simple Hello World program - I don't need to explain it here. So, I have naturally decided to compile my C code, and it is here that I come to a loss. I type in

gcc -o hello main.c

as told in this tutorial, but I come to an error:

gcc: error: main.c: No such file or directory

When I did the nano stuff, I came to a weird window, but I didn't know how to save what I wrote.

Thanks so much - this has been bugging me hugely.

  • 1
    Perfect storm is right, lol. The save on nano is ctrl-o, but it also asks you to save when you exit via ctrl-x. Learn to use the ls and stat command for reading directory contents, also maybe pwd and cd as you could be getting confused about where you are.
    – goldilocks
    Commented Mar 25, 2013 at 10:05
  • Nano is the easiest terminal text editor to use. There is vi or vim, or emacs but their use is a little tricky. Someday you'll have to learn them. But in the meanwhile nano is pretty straightfoward. Most important commands are listed in the bottom of the window, instead "ctrl" key, you will see a '^' character, so ^c means ctrl-C to cancel, ^x means ctrl-x to exit, and so on. Commented Feb 3, 2015 at 20:52
  • Another thing to note is that spaces in directory and file names should be avoided. You can use camel case or underscores/hyphens instead. Commented Jan 9, 2016 at 2:00

3 Answers 3


Do NOT use nano (or another text editor to put your code into) with root/sudo permissions (ie. do not edit with sudo nano, only use nano) if all you are doing is personal stuff that does not need superuser permissions.


To compile from the command line (assuming yourcode.c is the name of your C file, and program the name of the resulting program after compilation):

  1. Write your code in your favorite editor:

    • In the terminal, type nano yourcode.c (assuming you want to use nano);
    • Or use your Leafpad editor (and make sure to know where your file is saved).
  2. Back to the terminal, navigate to where your C file is stored. (Reminder: ls to list directory contents, cd to change directory.)

  3. Compile your code with gcc -o program yourcode.c.

  4. Execute it with ./program. Done!

Bonus method

If you intend on compiling/executing your program quite a lot, you may save yourself time by writing a Makefile. Create this file with Leafpad (or, in terminal, nano Makefile), then write:

    gcc -o program yourcode.c

(Make sure you presently use Tab for indents, and not spaces.) Then every time you just type make in the terminal (or make all, but let’s keep things short!), your program will compile and execute.


Want to make sure your GCC installation works? Copy-paste the following to your terminal:

cd /tmp
cat <<EOF > main.c
#include <stdio.h>

int main() {
    printf("Hello World\n");
    return 0;
gcc -o hello main.c
./hello # Press Enter to execute your program

If it echoes “Hello World”, then you’re good to go.

  • 1
    This is a really great tutorial and deserves way more upvotes. Commented Mar 6, 2017 at 20:08

Compiling C programs on the raspberry pi is rather simple. First, create your program in a text editor and save it as <insert name>.c It should be saved on the Desktop.

Next, open terminal. In it type:

cd Desktop

This changes the directory that the terminal is looking at to Desktop. This is also where our program is stored.

gcc -Wall <myName>.c -o <compiled name>

This is where the interesting stuff occurs. GCC is the compiler - it makes your code executable. -Wall activates compiler warnings - this is extremely helpful for debugging. The next line <myName>.c tells the computer where the code is stored. -o is an option - it tell GCC to compile. Lastly, <compiled name> is the name of your new program.

  • 2
    +1 if you add -Wall to this. Opening a terminal by default puts you in $HOME, not $HOME/Desktop, and most things don't save anything to Desktop (there may be some that do, but it is false to imply that in general a text editor saves a file to ~/Desktop. I've actually never seen anything put a file there, and I've been using linux for a long long time.)
    – goldilocks
    Commented Mar 25, 2013 at 10:06
  • To explain further: I believe ~/Desktop is used by window managers that allow drag and drop of files on the root window. That's all.
    – goldilocks
    Commented Mar 25, 2013 at 10:13
  • @goldilocks I added -Wall Commented Mar 25, 2013 at 17:03
  • @goldilocks: why add -Wall? That can be kind of a scary thing for beginners if you don't tell them why they suddenly have a bunch more errors.
    – Jacobm001
    Commented Jan 9, 2016 at 20:19
  • :/ Point taken about explaining it, I guess. But they should be using if they aren't yet no matter how new they are. The sooner, the better, and the best = every time including the first. Or maybe you figure they should wait a few years? I'd recommend -Wextra too, although it includes things that might be a bit irritating for beginners esp. w/ C++ (namely unused parameters in subclass methods). There's no such excuse for -Wall unless you are really dead set on using as few parentheses as possible.
    – goldilocks
    Commented Jan 9, 2016 at 20:32

Your "Not found" error is caused by not having a ./ in front. So:

gcc -o ./hello ./main.c

  • 1
    it would help if you explained what this does and how you new this was the problem. Commented Jan 9, 2016 at 1:59
  • Could use also add some code formatting, to differentiate the example solution from the text? Nevertheless, welcome to SE RPi - please take some time to have a look at the tour and take a look at some other people's answers to get a good idea of what constitutes a complete answer. Commented Jan 9, 2016 at 16:35
  • 1
    The ./ would not be needed.
    – Jacobm001
    Commented Jan 9, 2016 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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