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
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!
What is the correct way to compile c code
How to compile a c program by using gcc
gcc - How to compile C files in terminal - Raspberry Pi Stack Exchange
Does “C Programming: A Modern Approach” Show How To Compile?
Videos
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
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
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.
Title. I just started the book and got to chapter 2 where he talks about compiling and I’m not sure I fully understand or even see where he shows HOW to compile. Just says to use gcc with whatever text-editor I choose as far as I can tell. Maybe I’m just daft...
Edit: Forgot to mention, he does recommend using the command line early in the book. How would I go about doing this?
Edit 2: on Windows
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).
Im using VScode and whenever I try to run a program I get the error message:
"gcc : The term 'gcc' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."
I've tried troubleshooting and know you need something called MinGW or something but the website seems to be down so I'm not sure what to do. Any help is appreciated.
Sorry for being noob lol