PiGuy PiGuy - 1 month ago 7
C Question

How can I execute a C program without creating a copy of said file?

I hope your having a wonderful morning. I was on the Raspberry Pi website, and downloaded an issue of MagPi that taught you how to get started in the C language, and I'm so exited! The instructions told me to make a file, name it

hello.c
, and then put in the terminal command
gcc -o myprog /home/pi/Desktop/C/hello.c
to make the program executable. Once this step ended, it told me that to run it, you write
./myprog
, then it executes. When I changed the program up so it did something else, and I ran the code again, it printed the version that I hadn't changed up. When I ran the gcc command, It did change, but created a new project on the desktop for it. I don't want my raspberry flooded with hello world c programs, so if there is a way not to get it to clone itself, please let me know. A little long winded, but I hope it explains my situation.

Answer

Each time you compile the program, the generated executable is written in the folder you are in.

E.g. if you are in /home/pi/ And you run your command, you'll get an hello executable in the same folder

You use -o parameter to tell where to store the output executable, but you should provide a full path.

E.g.

to create the destination folder

mkdir -p /home/pi/Desktop/C/bin/

to build

gcc -o /home/pi/Desktop/C/bin/myprog /home/pi/Desktop/C/hello.c