joelindo
Posts: 26
Joined: Sun Aug 16, 2020 11:13 pm

Compile and Run, How Do I?

Mon Aug 17, 2020 12:20 am

I can create a program in C, but I don't know how compile and run it on Pi4B or a PiZeroW.

bassamanator
Posts: 91
Joined: Wed Jun 10, 2020 7:04 am

Re: Compile and Run, How Do I?

Mon Aug 17, 2020 2:41 am

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 |

User avatar
jahboater
Posts: 6663
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Compile and Run, How Do I?

Mon Aug 17, 2020 3:11 am

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, Pi1 Rev 1 256MB, Pi Zero

W. H. Heydt
Posts: 14376
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Compile and Run, How Do I?

Mon Aug 17, 2020 3:32 am

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

joelindo
Posts: 26
Joined: Sun Aug 16, 2020 11:13 pm

Re: Compile and Run, How Do I?

Mon Aug 17, 2020 4:03 pm

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.

joelindo
Posts: 26
Joined: Sun Aug 16, 2020 11:13 pm

Re: Compile and Run, How Do I?

Mon Aug 17, 2020 4:07 pm

I suppose learning Linux is the next step. . . Does Linux take the text file and compile it by itself?

User avatar
DougieLawson
Posts: 40781
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?

Mon Aug 17, 2020 4:16 pm

joelindo wrote:
Mon Aug 17, 2020 4:07 pm
I suppose learning Linux is the next step. . . Does Linux take the text file and compile it by itself?
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.

User avatar
jahboater
Posts: 6663
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Compile and Run, How Do I?

Mon Aug 17, 2020 5:30 pm

DougieLawson wrote:
Mon Aug 17, 2020 4:16 pm
joelindo wrote:
Mon Aug 17, 2020 4:07 pm
I suppose learning Linux is the next step. . . Does Linux take the text file and compile it by itself?
No. There nothing in stock Linux that would do that for you (without adding special scripts).
Yes there is, for simple programs anyway.

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!
$  
as you can see, make has found a matching source file with a ".c" extension, so it uses the C compiler to create the target program.
No makefile or script is needed for a simple program like this.
Pi4 8GB (Raspberry Pi OS 64-bit), Pi4 4GB, Pi1 Rev 1 256MB, Pi Zero

dsyleixa123
Posts: 1117
Joined: Mon Jun 11, 2018 11:22 am

Re: Compile and Run, How Do I?

Tue Aug 18, 2020 7:42 am

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 8-)
viewforum.php?f=33&sid=c3510c309cae6a9e3a832e3a42a7309e

joelindo
Posts: 26
Joined: Sun Aug 16, 2020 11:13 pm

Re: Compile and Run, How Do I?

Sun Sep 13, 2020 2:00 pm

Thanks All! The learning is slow and you've been great.

GlowInTheDark
Posts: 1344
Joined: Sat Nov 09, 2019 12:14 pm

Re: Compile and Run, How Do I?

Sun Sep 13, 2020 2:09 pm

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.
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.

User avatar
jahboater
Posts: 6663
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Compile and Run, How Do I?

Sun Sep 13, 2020 2:31 pm

GlowInTheDark wrote:
Sun Sep 13, 2020 2:09 pm
tcc is in the Raspbian repos - it runs on x86, x64, or ARM.
And aarch64 on Raspberry Pi OS 64-bit, just tried it.

Great little compiler!
Pi4 8GB (Raspberry Pi OS 64-bit), Pi4 4GB, Pi1 Rev 1 256MB, Pi Zero

W. H. Heydt
Posts: 14376
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Compile and Run, How Do I?

Sun Sep 13, 2020 4:35 pm

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.)

GlowInTheDark
Posts: 1344
Joined: Sat Nov 09, 2019 12:14 pm

Re: Compile and Run, How Do I?

Sun Sep 13, 2020 5:39 pm

W. H. Heydt wrote:
Sun Sep 13, 2020 4:35 pm
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.)
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.

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.

deepo
Posts: 697
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Compile and Run, How Do I?

Sun Sep 13, 2020 6:46 pm

joelindo wrote:
Mon Aug 17, 2020 4:03 pm
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.
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 write the changes to the file using Ctrl+O and exit the editor using Ctrl+X
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!
As jahboater describes, you can use the -o option to get an executable with the name you desire.

But for larger projects you should look into makefiles or even CMake.

/Mogens

User avatar
jahboater
Posts: 6663
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Compile and Run, How Do I?

Sun Sep 13, 2020 7:43 pm

GlowInTheDark wrote:
Sun Sep 13, 2020 5:39 pm
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.
You are supposed to use -O0 for fast debug/test cycles, and turn on optimization as production/release approaches.

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
TCC is a good bit faster than GCC, but then 105ms is not long to wait (for this trivial program anyway)!

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, Pi1 Rev 1 256MB, Pi Zero

joelindo
Posts: 26
Joined: Sun Aug 16, 2020 11:13 pm

Re: Compile and Run, How Do I?

Wed Sep 16, 2020 10:02 pm

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.

W. H. Heydt
Posts: 14376
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Compile and Run, How Do I?

Wed Sep 16, 2020 10:54 pm

GlowInTheDark wrote:
Sun Sep 13, 2020 5:39 pm
W. H. Heydt wrote:
Sun Sep 13, 2020 4:35 pm
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.)
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.

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.
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.

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.

Return to “Beginners”