Compile and Run, How Do I?
I can create a program in C, but I don't know how compile and run it on Pi4B or a PiZeroW.
-
- Posts: 91
- Joined: Wed Jun 10, 2020 7:04 am
Re: Compile and Run, How Do I?
Can you compile and run the C program on a non-RPi linux computer? I imagine the process would be the same. Likely need to use GCC.
| Raspberry Pi 4-8GB | 2.0GHz | over_voltage@5 |
| Raspberry Pi OS 32bit |
| https://github.com/bassamanator/raspberrypi-scripts |
| Raspberry Pi OS 32bit |
| https://github.com/bassamanator/raspberrypi-scripts |
Re: Compile and Run, How Do I?
If your program is called "hello.c" then the shortest command is:
Code: Select all
cc hello.c && ./a.out
Pi4 8GB (Raspberry Pi OS 64-bit), Pi4 4GB, Pi4 2GB, Pi1 Rev 1 256MB, Pi Zero
-
- Posts: 14393
- Joined: Fri Mar 09, 2012 7:36 pm
- Location: Vallejo, CA (US)
Re: Compile and Run, How Do I?
Depends on what resources your program needs...like libraries and such. For instance, here is a small selection from a makefile I have for recompiling my con reg system:
Code: Select all
CC = gcc
INCLUDES = -I/usr/include/mysql
LIBS = -L/usr/lib/mysql -lmariadbclient
all: ddc
ddc.o: ddc.c
$(CC) -c $(INCLUDES) ddc.c
ddc: ddc.o
$(CC) -o ddc ddc.o $(LIBS)
cp ddc /home/ddcadmin/bin
clean:
rm -f ddc*.o ddc
Re: Compile and Run, How Do I?
Sorry about the extra post. I'm new here and quite lost so far. . . I have compiled and run programs only on a Windows machine, never on Linux. I've never worked with Linux before.
Re: Compile and Run, How Do I?
I suppose learning Linux is the next step. . . Does Linux take the text file and compile it by itself?
- DougieLawson
- Posts: 40791
- Joined: Sun Jun 16, 2013 11:19 pm
- Location: A small cave in deepest darkest Basingstoke, UK
- Contact: Website Twitter
Re: Compile and Run, How Do I?
No. There nothing in stock Linux that would do that for you (without adding special scripts).
You're running a program to compile source code to object code and link object code to an executable.
The program that does that compilation is cc or gcc (or for C++ you'd run g++ or gpp).
https://medium.com/@salmenzouari/how-to ... 8ab78a5f53
Any language using left-hand whitespace for syntax is ridiculous
Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.
Any requirement to use a crystal ball or mind reading will result in me ignoring your question.
Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.
Any requirement to use a crystal ball or mind reading will result in me ignoring your question.
Re: Compile and Run, How Do I?
Yes there is, for simple programs anyway.DougieLawson wrote: ↑Mon Aug 17, 2020 4:16 pmNo. There nothing in stock Linux that would do that for you (without adding special scripts).
Just type "make program" and make will deduce how to build it and create the executable for you.
Code: Select all
$ ls
hello.c
$ make hello
cc hello.c -o hello
$ ./hello
Hello world!
$
No makefile or script is needed for a simple program like this.
Pi4 8GB (Raspberry Pi OS 64-bit), Pi4 4GB, Pi4 2GB, Pi1 Rev 1 256MB, Pi Zero
-
- Posts: 1117
- Joined: Mon Jun 11, 2018 11:22 am
Re: Compile and Run, How Do I?
the most simple way IMO is using the Geany IDE. It comes already installed with Stretch or Buster (look into the GUI menu) and it provides a convenient text editor for your file, you don't have to mess around with the annoying make/makefile thing. Geany can address the C/C++ compiler gcc which also already preinstalled on the Pi.
For compiling, building, and running you can just write the parameters into Geany's preferences:
compile: gcc -Wall -c "%f"
build: gcc -Wall -o "%e" "%f"
run: „sudo ./%e"
save the settings.
now Geany uses your active source code tab automatically and you can use short-cuts for
compile : [F8]
build: [F9}
run: [F5]
For further questions continue in the C/C++ sub-forum
viewforum.php?f=33&sid=c3510c309cae6a9e3a832e3a42a7309e
For compiling, building, and running you can just write the parameters into Geany's preferences:
compile: gcc -Wall -c "%f"
build: gcc -Wall -o "%e" "%f"
run: „sudo ./%e"
save the settings.
now Geany uses your active source code tab automatically and you can use short-cuts for
compile : [F8]
build: [F9}
run: [F5]
For further questions continue in the C/C++ sub-forum

viewforum.php?f=33&sid=c3510c309cae6a9e3a832e3a42a7309e
Re: Compile and Run, How Do I?
Thanks All! The learning is slow and you've been great.
-
- Posts: 1353
- Joined: Sat Nov 09, 2019 12:14 pm
Re: Compile and Run, How Do I?
Another cute solution (literal solution to the problem as phrased in the Subject line) is tcc - the tiny C compiler. tcc is very fast; much faster (at compiling) than gcc.
For example:
(Note: As far as I can tell, you can't do this with gcc - and, believe me, I've tried...)
$ tcc -run - <<< 'int main(void) { puts("hello, world"); }'
hello, world
$
tcc is in the Raspbian repos - it runs on x86, x64, or ARM.
tcc is actually pretty cool. It is also possible, with tcc, to write a C program that accepts another C program as input. It will then compile and run that C program (without forking another process; that would be cheating).
Incidentally, as far as I can tell, you can't do both of these tricks at once, though. I.e., you can't use -run with a program that does what is described in the previous paragraph. You have to create an output file and run that.
For example:
(Note: As far as I can tell, you can't do this with gcc - and, believe me, I've tried...)
$ tcc -run - <<< 'int main(void) { puts("hello, world"); }'
hello, world
$
tcc is in the Raspbian repos - it runs on x86, x64, or ARM.
tcc is actually pretty cool. It is also possible, with tcc, to write a C program that accepts another C program as input. It will then compile and run that C program (without forking another process; that would be cheating).
Incidentally, as far as I can tell, you can't do both of these tricks at once, though. I.e., you can't use -run with a program that does what is described in the previous paragraph. You have to create an output file and run that.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth
Loves Linux; loves to dance.
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth
Loves Linux; loves to dance.
Re: Compile and Run, How Do I?
And aarch64 on Raspberry Pi OS 64-bit, just tried it.GlowInTheDark wrote: ↑Sun Sep 13, 2020 2:09 pmtcc is in the Raspbian repos - it runs on x86, x64, or ARM.
Great little compiler!
Pi4 8GB (Raspberry Pi OS 64-bit), Pi4 4GB, Pi4 2GB, Pi1 Rev 1 256MB, Pi Zero
-
- Posts: 14393
- Joined: Fri Mar 09, 2012 7:36 pm
- Location: Vallejo, CA (US)
Re: Compile and Run, How Do I?
All various ways to do what we used to call "compile, load, and blow". (Which was then followed by reading the core dump to find out what went wrong.)
-
- Posts: 1353
- Joined: Sat Nov 09, 2019 12:14 pm
Re: Compile and Run, How Do I?
Yeah, that actually points to one of the design goals/ideas behind tcc - that it is good for development (I.e., endless edit/compile/test/repeat cycles) because it compiles fast. The emphasis is on compiling fast, rather than on generating fast (efficient) code. gcc is just the opposite; slow compiler that generates very good code.W. H. Heydt wrote: ↑Sun Sep 13, 2020 4:35 pmAll various ways to do what we used to call "compile, load, and blow". (Which was then followed by reading the core dump to find out what went wrong.)
It is generally held, in particular, that such a compiler is good for students, since they compile often, but once working, their code is pretty much never used.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth
Loves Linux; loves to dance.
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth
Loves Linux; loves to dance.
Re: Compile and Run, How Do I?
It's quite simple, but in the end you will need to learn Linux to progress. But that can come later.
Log in to your Raspberry Pi, then start the nano editor:
nano helloworld.c
Then type in the hello world code:
Code: Select all
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello world!\n");
return 0;
}
Then compile the code (the compiler is already installed):
cc helloworld.c
The output of the compilation is in a.out per default, now run it:
./a.out
And you should see:
Code: Select all
Hello world!
But for larger projects you should look into makefiles or even CMake.
/Mogens
Re: Compile and Run, How Do I?
You are supposed to use -O0 for fast debug/test cycles, and turn on optimization as production/release approaches.GlowInTheDark wrote: ↑Sun Sep 13, 2020 5:39 pmYeah, that actually points to one of the design goals/ideas behind tcc - that it is good for development (I.e., endless edit/compile/test/repeat cycles) because it compiles fast. The emphasis is on compiling fast, rather than on generating fast (efficient) code. gcc is just the opposite; slow compiler that generates very good code.
Code: Select all
pi@raspberrypi:~ $ time tcc hello.c
real 0m0.027s
user 0m0.020s
sys 0m0.008s
pi@raspberrypi:~ $pi@raspberrypi:~ $ time gcc -O3 hello.c
real 0m0.105s
user 0m0.073s
sys 0m0.032s
GCC fully supports the latest standards: C18, C++17, and the draft C2x and C++20, which although TCC is very good in this respect, likely will not match.
The -run option is great too, but "gcc hello.c && ./a.out" is easy enough in a script called perhaps "ccr".
Oddly, "gcc hello.c && ./a.out" is faster than running a similar Python program ......explain that!!!
Pi4 8GB (Raspberry Pi OS 64-bit), Pi4 4GB, Pi4 2GB, Pi1 Rev 1 256MB, Pi Zero
Re: Compile and Run, How Do I?
Sorry for taking so long to respond. Been busy learning on my own, which, along with all your help, has fixed my problem. Thanks again.
-
- Posts: 14393
- Joined: Fri Mar 09, 2012 7:36 pm
- Location: Vallejo, CA (US)
Re: Compile and Run, How Do I?
Yes and no. If you only get one shot at compiling and failing per day (unless you're on especially good terms with the console operators), it tends to make one really check the code to get the most out of each run. On the other hand, more or less immediate feedback has it's advantages as well.GlowInTheDark wrote: ↑Sun Sep 13, 2020 5:39 pmYeah, that actually points to one of the design goals/ideas behind tcc - that it is good for development (I.e., endless edit/compile/test/repeat cycles) because it compiles fast. The emphasis is on compiling fast, rather than on generating fast (efficient) code. gcc is just the opposite; slow compiler that generates very good code.W. H. Heydt wrote: ↑Sun Sep 13, 2020 4:35 pmAll various ways to do what we used to call "compile, load, and blow". (Which was then followed by reading the core dump to find out what went wrong.)
It is generally held, in particular, that such a compiler is good for students, since they compile often, but once working, their code is pretty much never used.
When dealing with student programs, I doubt that you'll see any that take gcc very long to compile. I'd be surprised to see a student program that took long enough to compile to go get a cup of tea.