8

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

Background

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.

xxmbabanexx
  • 3,278
  • 7
  • 36
  • 56

3 Answers3

16

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.

Answer

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:

all:
    gcc -o program yourcode.c
    ./program

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


Testing

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;
}
EOF
gcc -o hello main.c
./hello # Press Enter to execute your program

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

Diti
  • 310
  • 2
  • 11
4

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.

xxmbabanexx
  • 3,278
  • 7
  • 36
  • 56
-3

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

gcc -o ./hello ./main.c

Jacobm001
  • 11,904
  • 7
  • 47
  • 58
Sam
  • 1