Friday, March 20, 2020
Start Programming C With raspberry Pi
Start Programming C With raspberry Pi This set of instructions wont suit everybody but Ill try to be as generic as possible. I installed the Debian Squeeze distribution, so the programming tutorials are based on that. Initially, Im starting off by compiling programs on the Raspi but given its relative slowness to any PC in the last ten years, its probably best to switch to developing on another PC and copying the executables over. Ill cover that in a future tutorial, but for now, its about compiling on the Raspi. Preparing for Developing The starting point is you have a Raspi with a working distribution. In my case, its Debian Squeeze which I burnt with instructions from the RPI Easy SD Card Setup. Make sure you bookmark the Wiki as its got tons of useful stuff. If your Raspi has booted and youve logged in (username pi, p/w raspberry) then type gcc - v at the command line. Youll see something like this: Using built-in specs.Target: arm-linux-gnueabiConfigured with: ../src/configure -v with-pkgversionDebian 4.4.5-8 with-bugurlfile:///usr/share/doc/gcc-4.4/README.Bugsenable-languagesc,c,fortran,objc,obj-c prefix/usr program-suffix-4.4 enable-shared enable-multiarch enable-linker-build-idwith-system-zlib libexecdir/usr/lib without-included-gettext enable-threadsposix with-gxx-include-dir/usr/include/c/4.4 libdir/usr/libenable-nls enable-clocalegnu enable-libstdcxx-debug enable-objc-gc disable-sjlj-exceptions enable-checkingrelease buildarm-linux-gnueabihostarm-linux-gnueabi targetarm-linux-gnueabiThread model: posixgcc version 4.4.5 (Debian 4.4.5-8) Install Samba One of the first things I did and recommend to you if you have a Windows PC on the same network as your Raspi is to install and setup Samba so you can access the Raspi. Then I issued this command: gcc -v l.txt To get the above listing into the file l.txt that I could view and copy on my Windows PC. Even if you are compiling on the Raspi, you can edit source code from your Windows box and compile on the Raspi. You cant just compile on your Windows box using say MinGW unless your gcc is configured to output ARM code. That can be done but lets learn to walk first and learn how to compile and run programs on the Raspi. Read up on Cygwin and MinGW. GUI or Terminal Ill assume that you are new to Linux, so apologies if you know it already. You can do most of the work from the Linux terminal ( command line). But it can be easier if you fire up the GUI (Graphical User Interface) to have a look around the file system. Type startx to do that. The mouse cursor will appear and you can click in the bottom left-hand corner (it looks like a mountain( to see the menus. Click on Accessories and run File Manager to let you view folders and files. You can close it down any time and return to the terminal by clicking the little red button with a white circle in the bottom right-hand corner. Then click on Logout to return to the command line. You may prefer to have the GUI open all the time. When you want a terminal click the bottom left button then click Other on the menu and Terminal. In the Terminal, you can close it by typing Exit or click the Windows like x in the top right-hand corner. Folders The Samba instructions on the Wiki tell you how to setup a public folder. Its probably best to do that. Your home folder (pi) will be readonly and you want to write to the public folder. I created a sub-folder in public called code and created the hello.c file listed below in it from my Windows PC. If you prefer to edit on the PI, it comes with a text editor called Nano. You can run it from the GUI on the other menu or from the terminal by typing sudo nanosudo nano hello.c The sudo elevates nano so it can write files with root access. You can run it just as nano, but in some folders that wont give you write access and you wont be able to save files so running things with sudo is usually best. Hello World Heres the code: #includeint main() {printf(Hello World\n);return 0;} Now type in gcc -o hello hello.c and it will compile in a second or two. Take a look at the files in the terminal by typing in ls -al and youll see a file listing like this: drwxrwxx 2 pi users 4096 Jun 22 22:19 .drwxrwxr-x 3 root users 4096 Jun 22 22:05 ..-rwxr-xr-x 1 pi pi 5163 Jun 22 22:15 hello-rw-rw 1 pi users 78 Jun 22 22:16 hello.c and type in ./hello to execute the compiled program and see Hello World. That completes the first of the programming in C on your Raspberry Pi tutorials. Into games programming in C? Try our free Games programming in C Tutorials.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.