How to compile C files in terminal

  • Question

    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.

    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.

    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.

    Another thing to note is that spaces in directory and file names should be avoided. You can use camel case or underscores/hyphens instead.

  • xxmbabanexx

    xxmbabanexx Correct answer

    7 years ago

    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.

    +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.)

    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 I added `-Wall`

    @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.

    :/ 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.

    @Jacobm001 I do draw the line at `-Wpedantic`...

    @goldilocks: I'm all for using -Wall, but since most tutorials don't use it, and books often encourage less than ideal practices, I avoid it in places like this. If it's given to a user here, I just think it needs to come with a disclaimer.

    Actually I just noticed there is an explanation of `-Wall` in the answer \O/ IMO you are not ever doing anyone a favor by neglecting to tell them about warnings given the chance. In all the languages/compilers/interpreters I've ever used the base level (e.g. `-Wall`) is akin to errors in the sense that they are never ambiguous or unavoidable; without exception properly written code in C can/will pass `-Wall`. That being the case, there's no point in not using it, or in educating someone to write bad code for no reason. You're better off learning to do things right from the start.

  • 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.

    This is a really great tutorial and deserves way more upvotes.

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

    gcc -o ./hello ./main.c

    it would help if you explained what this does and how you new this was the problem.

    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.

    The `./` would not be needed.

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM