Do NOT use
nano(or another text editor to put your code into) with root/sudo permissions (ie. do not edit withsudo nano, only usenano) 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):
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).
- In the terminal, type
Back to the terminal, navigate to where your C file is stored. (Reminder:
lsto list directory contents,cdto change directory.)Compile your code with
gcc -o program yourcode.c.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.
Answer from Diti on Stack ExchangeVideos
Do NOT use
nano(or another text editor to put your code into) with root/sudo permissions (ie. do not edit withsudo nano, only usenano) 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):
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).
- In the terminal, type
Back to the terminal, navigate to where your C file is stored. (Reminder:
lsto list directory contents,cdto change directory.)Compile your code with
gcc -o program yourcode.c.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.
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.
Try to compile your code normally as
gcc test.c
If you get default output file a.exe,then go for
gcc test.c -o test.exe
I would suggest you go through this compilation instruction :-
gcc -o test.exe test.c
I believe this code runs perfectly on your windows system.Please inform if it doesn't!
You need to run
Copygcc b.c -o b.exe
Without -o option it'll use the default output executable name which is a.exe on Windows and a.out on *nix systems. You can easily see the a.exe with the dir command
-o file
Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.
If
-ois not specified, the default is to put an executable file ina.out, the object file for source.suffix insource.o, its assembler file insource.s, a precompiled header file insource.suffix.gch, and all preprocessed C source on standard output.https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html
For more information read Determining C executable name
Of course you can also run a instead of b
Compile like this
Copygcc -o b b.c
by specifying output filename
Copy-o <output_file>