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
Answer from Jeremy Kerr on askubuntu.comYou 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).
Compile your source file prog.c with:
$ gcc prog.c
this will generate an executable named a.out. Execute it with:
$ ./a.out
To specify the name of the executable during compilation:
$ gcc prog.c -o prog
execute with:
$ ./prog
gcc is also the C++ compiler. There are plenty command line options available, so it's worth getting to know the man page.
Here is the manual for the most recent version of the compiler.
Find a tutorial on the Gnu Compiler Collection and read it all trying examples on an hello world program. Make obvious errors to get used to the error messages that it spits out and look them up. There is a wealth of knowledge online about different messages. You can copy and past them into google and find good results.
A good book would be (An Introduction to GCC) by Brian Gough :ISBN 0-9541617-9-3:
One thing you should always include in compiling is the -Wall flags(the W is in caps). It tells gcc to spit out the most common error messages.
geany is the text editor I uses as it is light weight fast and includes just about anything you could need in an IDE.
After you get used to gcc check out make as they go hand in hand for projects. What make does is provide a way to compile everything with out having to remember what libs need to be manually included for linked on compile time form the terminal. It also makes you life easier and easier as a project gets more complicated.
How to compile C program with variable name using Linux terminal? - Stack Overflow
What is the correct way to compile c code
How to compile C program developed with Microsoft Visual C/C++ 6.0 in Linux system
I'm on linux. How do i compile C code to a .exe file when it automatically compiles to .out?
Videos
As mentioned in the comments you can already do
Copymake file_name
and it will, using default build rules (so you don't even need a Makefile), build an executable called "file_name" if there is a C source file called "file_name.c"
if you really do need the executable to have the .out extension you could create a Makefile that looks like this
CopyCFLAGS=-g -Wall
%.out: %.c
(CFLAGS) -o
^
This defines a rule that for anything that ends in ".out" it will build it from the corresponding ".c" file. So if you type
Copymake file_name.out
as above it will build "file_name.out" if you have a "file_name.c"
Just create a Makefilewhich should contain the command as you shown
Copygcc -g -Wall file_name.c -o file_name.out
Place this Makefile in the directory where the source code is there.
Then open the command line and change the directory to the location where this Makefile is there.
Then execute this command
Copymake
No need to give make argument.c
I've got a program that is split into header files and c files along with the main. I was told to do gcc main.c <library.c>... for every other code but i don't remember having to do that when I've done stuff with header files in the past. How are you supposed to do this. Also are you supposed to include the headerfile in the .c version that the header is declaring the functions from? Sorry for my lack of technical terms. I'm exhausted today
i'm pretty new so don't bully me lmao
Compiling a C program has always been kind of a messy process for me As in, I don't really understand what I'm doing, I just copy and paste the command for compiling it.
I was using mingw for a while, but I recently found out that it does much more stuff than just compile a C program. I assume the main part is the gcc executable, but what are the ways to get it?
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.