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 Exchangegcc - How to compile C files in terminal - Raspberry Pi Stack Exchange
How do I run my C program in Mac Terminal?
How do I execute a .c file? - Stack Overflow
Using GCC to compile C code - Stack Overflow
What Does the -static Flag Do When Compiling a C Program?
How Do You Cross-Compile a C Program for a Different Architecture?
How Can You Include Debugging Symbols in a Compiled C Program?
Videos
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.
I'm trying to run a file that I made in the Mac Terminal. How do I do this? I don't really understand how to. I wrote my file in emacs in the terminal, if that makes sense/helps.
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 compile your program before you can run it. To do this, you'll need a C compiler, like gcc. You can install this with:
sudo apt-get install gcc
Then, to compile your program, creating an executable called file:
gcc -Wall -o file file.c
Which you should then be able to run:
./file
Fabrice Bellard's TCC seems to be still in the repositories. It can run in a kind of interpreter-mode which makes the following possible:
You can make a simple C-file executable like the OP tried to do by adding the line
#!/usr/bin/tcc -run
to the very top of the file.
It also accepts input from STDIN by adding an empty option (just the minus sign -) at the end.
$ /usr/bin/tcc -run - <<EOF
> #include <stdio.h>
> int main()
> {
> printf("Hello World\n");
> return 0;
> }
> EOF
Hello World
or with echo
echo '#include <stdio.h> int main(){printf("Hello World\n");return 0;}' | /usr/bin/tcc -run -
or just run /usr/bin/tcc -run - type your code and start the run with CTRL + D
Seems useless and silly but the last method is the fastest (for me, YMMV etc.) to check for a function in a large library, look up the exact value of a constant etc. And it is small (180k) which makes it a good fit for e.g. the Raspberry-Pi.
Main disadvantage: development stopped (last version is from 2013).