{"id":177,"date":"2012-10-10T11:45:13","date_gmt":"2012-10-10T11:45:13","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=177"},"modified":"2015-06-12T05:29:35","modified_gmt":"2015-06-12T05:29:35","slug":"getting-your-raspberry-pi-ready-for-c-programming","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/getting-your-raspberry-pi-ready-for-c-programming","title":{"rendered":".Getting Your RPi Ready For C Programming"},"content":{"rendered":"<p>\nC is a natural choice to program in on the Raspberry Pi. It&#39;s very powerful, usable on virtually all hardware platforms and really similar to lots of other programming languages such as Java, PHP, C# and objective C. As popular programming languages go its as powerful as it gets, with only assembler beating it in terms of programming to the bare metal of a hardware platform.&nbsp; Its also what Linux itself is written in.\n<\/p>\n<p>\nThe notes on this site are largely focused on programming in C and C++ and we typically do this running on the Raspbian operating system.&nbsp; If you are a beginner don&#39;t worry about the distinction between C and C++ too much.&nbsp; You can start programming in a C++ project just using C and move into using the more fancy C++ concepts later on &#8211; you don&#39;t have to choose one or the other at the outset.&nbsp; Once you start using some of the higher level features of C++ you can cause problems by also using straight C, but by the time you are comfortable doing that you should have learnt enough about the language to avoid those sorts of issues so don&#39;t worry about it at the outset.\n<\/p>\n<h4>\nGetting Started On Your Raspberry Pi For C &amp; C++ Programming<br \/>\n<\/h4>\n<p>\nHere&#39;s our step by step guide to getting up and running for C development on a Raspberry Pi using an IDE (Integrated Development Environment) so you don&#39;t have to program using the Linux command line.\n<\/p>\n<p>\n1) Download the Raspbian Wheezy SD card image from the Raspberry Pi website <a href=\"http:\/\/www.raspberrypi.org\/downloads\" target=\"_blank\">downloads page<\/a>\n<\/p>\n<p>\n2) Install it on a SD card using <a href=\"https:\/\/raspberry-projects.com\/pi\/pi-operating-systems\/win32diskimager\" target=\"_blank\">win32diskimager<\/a> (if you are running Windows) or use the new noobs installer.\n<\/p>\n<p>\n3) Boot your RPi, <a href=\"https:\/\/raspberry-projects.com\/pi\/pi-operating-systems\/raspbian\/raspbian-general\" target=\"_blank\">log in and start the GUI<\/a>\n<\/p>\n<p>\n4) <a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/compilers-and-ides\/geany\/installing-geany\" target=\"_blank\">Geany<\/a>&nbsp;will already be installed.\n<\/p>\n<p>\n5) If you want to control the IO pins of your Raspberry Pi <a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/c-libraries\/bcm2835-by-mike-mccauley\" target=\"_blank\">install the bcm2835 library by Mike McCauley<\/a>\n<\/p>\n<p>\n5) Create a new <a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/compilers-and-ides\/geany\/creating-a-project\" target=\"_blank\">Geany project<\/a>\n<\/p>\n<p>\nThat&#39;s it, happy coding!\n<\/p>\n<h4>\nProgramming Your Raspberry Pi From Windows<br \/>\n<\/h4>\n<p>\nThis is very much do-able and the best solution for this we&#39;ve found is using&nbsp;NetBeans, see <u><a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/compilers-and-ides\/netbeans-windows\/installing-netbeans-for-c-remote-development-on-a-raspberry-pi\">here<\/a><\/u>.\n<\/p>\n<h4>\n<span style=\"color:#A9A9A9;\">How Do We Used To&nbsp;Do It (Before NetBeans &#8211; This section is here still just&nbsp;for archive reasons)?<\/span><br \/>\n<\/h4>\n<p>\n<span style=\"color:#A9A9A9;\"><em>[Update 2015-02: This section was written before the release of the Raspberry Pi 2, which makes programming&nbsp;in an IDE directly on the Raspberry Pi a more realistic proposition with the version 2 performance boost. &nbsp;The method of&nbsp;programming mentioned&nbsp;here is still very useful though if you want to be able to edit your code from a powerful PC running Windows etc]<\/em><\/span>\n<\/p>\n<p>\n<span style=\"color:#A9A9A9;\">We work primarily on Windows and our ideal is to be able to program using a good&nbsp;Windows IDE (Integrated Development Environment application). &nbsp;We do that&nbsp;to program all sorts of embedded micro controllers and other processor boards so&nbsp;for us its an obvious choice to make for programming on the RPi too. &nbsp;However we&#39;ve not yet found the perfect solution. You&#39;ll notice in this section of the site various different approaches are documented and this is simply us making notes as we&#39;ve tried different things searching for an optimum solution and learning&nbsp;the hard way (for instance Linux heads in forums swearing&nbsp;QEMU is the way to go, but&nbsp;finding out after a day of banging our head against a wall its seriously flawed until someone uber expert creates a more powerful hardware file for it).<\/span>\n<\/p>\n<p>\n<span style=\"color:#A9A9A9;\">The main problem &#8211; the RPi&nbsp;is amazing but its slow for an IDE and efficient fast desktop workers. &nbsp;Sure you can program on the command line (and you&#39;ll find plenty of Linux heads telling you its best) but like marmite&nbsp;only certain people can bear to program in such a confined way and its definitely not for us. If you are new to programming then using Geany&nbsp;on the RPi is a brilliant way to get going creating a single file program or a multiple file program using a makefile like the one on <\/span><a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/compilers-and-ides\/geany\/creating-a-project\" target=\"_blank\"><span style=\"color:#A9A9A9;\">this page<\/span><\/a><span style=\"color:#A9A9A9;\">. Once you get into programming large projects the sluggishness of the RPi platform is likely going to annoy you though. Its not just that the basic text editor in Geany&nbsp;is sluggish, its that you are likely also switching between Geany and web pages, pdf&#39;s, etc and having to wait for the OS to catch up all the time can be a massive pain.<\/span>\n<\/p>\n<p>\n<span style=\"color:#A9A9A9;\">This is how we&#39;ve dealt with it for now (we keep looking for new better ways):<\/span>\n<\/p>\n<p style=\"margin-left: 40px;\">\n<span style=\"color:#A9A9A9;\">For projects that are using standard C libraries and simple&nbsp;to copy over RPi libraries we use eclipse on Windows with a cross compiler solution as documented <\/span><a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/compilers-and-ides\/eclipse\/programming-the-rpi-in-windows-using-eclipse\"><span style=\"color:#A9A9A9;\">here<\/span><\/a><span style=\"color:#A9A9A9;\">.<\/span>\n<\/p>\n<p style=\"margin-left: 40px;\">\n<span style=\"color:#A9A9A9;\">When we&#39;re programming applications using libraries with a lot of dependencies (e.g. GTK+), or if working on a PC where we don&#39;t&nbsp;want to go through the arduous&nbsp;process of setting up eclipse for windows cross compiler &nbsp;programming we do the following to do &quot;sort of&quot; windows programming with eclipse!<\/span>\n<\/p>\n<p style=\"margin-left: 80px;\">\n<span style=\"color:#A9A9A9;\">We setup a network file share on the RPi using&nbsp;SMB as documented <\/span><a href=\"https:\/\/raspberry-projects.com\/pi\/software_utilities\/network-file-share-using-smb\" target=\"_blank\"><span style=\"color:#A9A9A9;\">here<\/span><\/a><span style=\"color:#A9A9A9;\">. This lets us access the RPi&nbsp;via windows networking (it&nbsp;becomes a&nbsp;network device in Windows explorer)<\/span>\n<\/p>\n<p style=\"margin-left: 80px;\">\n<span style=\"color:#A9A9A9;\">We create a project in Geany on the RPi (various .c and .h files with the&nbsp;makefile&nbsp;on&nbsp;<\/span><a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/compilers-and-ides\/geany\/creating-a-project\" target=\"_blank\"><span style=\"color:#A9A9A9;\">this page<\/span><\/a><span style=\"color:#A9A9A9;\">.<\/span>\n<\/p>\n<p style=\"margin-left: 80px;\">\n<span style=\"color:#A9A9A9;\">We then close Geany and run Eclipse on Windows. &nbsp;We open the RPi .c and .h files in eclipse via the network drive and use eclipse to write our code.<\/span>\n<\/p>\n<p style=\"margin-left: 80px;\">\n<span style=\"color:#A9A9A9;\">We save the files using CTRL+SHIFT+S for save all and then we build the project from the RPi command line, or using&nbsp;LX Terminal if we&#39;re using the desktop, by moving to the project directory (e.g. cd projects\/my_project\/ ) and entering:<\/span>\n<\/p>\n<pre style=\"margin-left: 80px;\">\r\n<span style=\"color:#A9A9A9;\"><code>\r\nmake\r\n<\/code><\/span><\/pre>\n<p style=\"margin-left: 80px;\">\n<span style=\"color:#A9A9A9;\">The results of the make are shown and if there are errors we have to reference them by line number in the files (eclipse show line numbers option is in:&nbsp;Menu &gt; preference &gt; general &gt; editors &gt; text editors &gt; show line numbers).<\/span>\n<\/p>\n<p style=\"margin-left: 80px;\">\n<span style=\"color:#A9A9A9;\">Once we have a successful build we run the application on the RPi&nbsp;from the command line or LX Terminal using<\/span>\n<\/p>\n<pre style=\"margin-left: 80px;\">\r\n<span style=\"color:#A9A9A9;\"><code>\r\nsudo .\/my_application.a\r\n<\/code><\/span><\/pre>\n<p style=\"margin-left: 80px;\">\n<span style=\"color:#A9A9A9;\">Its not ideal as the compiler is slower running on the RPi, we can&#39;t just double click on errors to be taken to the offending line in our code and we can&#39;t debug by stepping through the code, but the benefit of programming in a fast editor on a fast PC and getting to use the PRi with its amazing capabilities from the Linux OS and libraries makes this a pretty good compromise solution a lot of the time.<\/span>\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C is a natural choice to program in on the Raspberry Pi. It&#39;s very powerful, usable on virtually all hardware platforms and really similar to lots of other programming languages such as Java, PHP, C# and objective C. As popular programming languages go its as powerful as it gets, with only assembler beating it in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,9],"tags":[],"class_list":["post-177","post","type-post","status-publish","format-standard","hentry","category-compilers-and-ides","category-programming-in-c"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=177"}],"version-history":[{"count":78,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/177\/revisions"}],"predecessor-version":[{"id":2070,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/177\/revisions\/2070"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}