{"id":2038,"date":"2016-10-12T16:16:25","date_gmt":"2016-10-12T23:16:25","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=2038"},"modified":"2016-10-12T16:20:35","modified_gmt":"2016-10-12T23:20:35","slug":"developing-c-projects-for-raspberry-pi-zero-from-windows","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/raspberry\/zero\/","title":{"rendered":"Developing C++ Projects for Raspberry Pi Zero from Windows"},"content":{"rendered":"<p>This tutorial shows how to create C++ projects for the ultra-low-cost Raspberry Pi Zero board using Visual Studio, VisualGDB and an automatically installed cross-toolchain.<\/p>\n<p>We will show how to configure the Pi to act as a USB network device and how to access this device from Windows and upload your programs there. Then we will make a basic C++ 11 program that will test the performance of the <strong>sin()<\/strong> function and will use it to compare the speed of Raspberry Pi Zero vs. Raspberry Pi 3.<\/p>\n<p>Before you begin, install <a href=\"http:\/\/visualgdb.com\/download\/\">VisualGDB 5.1<\/a> or later.<\/p>\n<ol>\n<li>Download the latest Raspberry Pi image and use <a href=\"http:\/\/winflashtool.sysprogs.com\/\">WinFLASHTool <\/a>to write it on an SD card (Raspberry Pi Zero uses the same images as Raspberry Pi 2 or 3). Ensure you check the &#8220;Resize the last Ext2FS partition&#8221; checkbox to make use of the full capacity of your SD card:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/00-flash.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2039\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/00-flash.png\" alt=\"00-flash\" width=\"626\" height=\"475\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/00-flash.png 626w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/00-flash-300x228.png 300w\" sizes=\"(max-width: 626px) 100vw, 626px\" \/><\/a><\/li>\n<li>Raspberry Pi Zero does not have an on-board network connector, so if you start it from the unmodified SD card image, you will not be able to connect to it. We will fix that by switching its USB port from the USB host mode to USB device mode. Open the <strong>&lt;SD CARD drive&gt;:\\config.txt<\/strong> file in a text editor and add the following line to the end of it:\n<pre class=\"\">dtoverlay=dwc2<\/pre>\n<p><a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-dwc2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2040\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-dwc2.png\" alt=\"01-dwc2\" width=\"832\" height=\"428\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-dwc2.png 832w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-dwc2-300x154.png 300w\" sizes=\"(max-width: 832px) 100vw, 832px\" \/><\/a><\/li>\n<li>Now open the <strong>&lt;SD card drive&gt;:\\cmdline.txt<\/strong> file and insert &#8220;<strong>modules-load=dwc2,g_ether<\/strong>&#8221; just after the &#8220;rootwait&#8221; word:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-modules.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2041\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-modules.png\" alt=\"02-modules\" width=\"832\" height=\"428\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-modules.png 832w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-modules-300x154.png 300w\" sizes=\"(max-width: 832px) 100vw, 832px\" \/><\/a>This will load the &#8220;ethernet device&#8221; driver, so the Raspberry Pi Zero will appear as a USB ethernet controller.<\/li>\n<li>Now you can insert the SD card into your Raspberry Pi Zero and plug the a USB cable into the &#8220;USB&#8221; connector:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/board.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2053\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/board.jpg\" alt=\"board\" width=\"700\" height=\"410\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/board.jpg 700w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/board-300x176.jpg 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/li>\n<li>The green LED on the board will start blinking sporadically and turning off for prolonged times. This is normal &#8211; the board will start in approximately 30 seconds and you should see a new network device in your Device Manager:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/gadget.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2054\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/gadget.png\" alt=\"gadget\" width=\"781\" height=\"572\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/gadget.png 781w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/gadget-300x220.png 300w\" sizes=\"(max-width: 781px) 100vw, 781px\" \/><\/a><\/li>\n<li>Unless you specially install and configure a DHCP server on your computer, both Raspberry Pi and your PC will get temporary addresses from the <strong>169.254.x.x<\/strong> range. However finding out the exact address of your Raspberry Pi will require one extra step. First check if your computer has the &#8220;Bonjour&#8221; service installed by running <strong>services.msc<\/strong>:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-bonjour.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2042\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-bonjour.png\" alt=\"03-bonjour\" width=\"806\" height=\"593\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-bonjour.png 806w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-bonjour-300x221.png 300w\" sizes=\"(max-width: 806px) 100vw, 806px\" \/><\/a>If it is not installed, download and install the <a href=\"https:\/\/support.apple.com\/kb\/DL999\">Apple Bonjour Service for Windows<\/a>. This service will use the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bonjour_(software)\">Bonjour protocol<\/a> to automatically locate your Raspberry Pi Zero.<\/li>\n<li>Run &#8220;ping -4 raspberrypi.local&#8221; to check if your Raspberry Pi was discovered:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-ping.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2043\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-ping.png\" alt=\"04-ping\" width=\"663\" height=\"342\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-ping.png 663w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-ping-300x155.png 300w\" sizes=\"(max-width: 663px) 100vw, 663px\" \/><\/a>If the &#8216;ping&#8217; command does not work, ensure that the Bonjour service is installed and running.<\/li>\n<li>Now we will create a basic application and run it on Raspberry Pi Zero. Start Visual Studio and launch the VisualGDB Linux Project Wizard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-newprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2044\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-newprj.png\" alt=\"05-newprj\" width=\"786\" height=\"443\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-newprj.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-newprj-300x169.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>Proceed with the default &#8220;Create a new project -&gt; Application -&gt; MSBuild&#8221; settings and select C++11 as the language standard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-msbuild.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2045\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-msbuild.png\" alt=\"06-msbuild\" width=\"822\" height=\"662\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-msbuild.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-msbuild-300x242.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>On the next page select &#8220;Build the project locally with a cross-compiler&#8221; and choose the Raspberry Pi toolchain. If it is not installed yet, VisualGDB will download and install it automatically. Then choose &#8220;pi@raspberrypi.local&#8221; as the deployment computer. If it does not appear in the list, select &#8220;Create a new SSH connection&#8221; and specify the host name, user and password there. The default password for the &#8220;pi&#8221; user is &#8220;raspberry&#8221;.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-target.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2046\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-target.png\" alt=\"07-target\" width=\"822\" height=\"662\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-target.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-target-300x242.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>When you press &#8220;Finish&#8221;, VisualGDB will automatically test the toolchain and verify that it can upload and run programs on your board:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-test.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2047\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-test.png\" alt=\"08-test\" width=\"470\" height=\"364\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-test.png 470w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-test-300x232.png 300w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/a><\/li>\n<li>Once the project is created, replace the contents of the main cpp file with this:\n<pre class=\"\">#include &lt;iostream&gt;\r\n#include &lt;chrono&gt;\r\n#include &lt;math.h&gt;\r\n\r\nusing namespace std;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n\u00a0\u00a0\u00a0 auto start = std::chrono::system_clock::now();\r\n\u00a0\u00a0\u00a0 for (int i = 0; i &lt; 1000000; i++)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 volatile double arg = i * 0.001;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 volatile int x = sin(arg);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 auto end = std::chrono::system_clock::now();\r\n\u00a0\u00a0\u00a0 std::chrono::duration&lt;double&gt; elapsed = end - start;\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 std::cout &lt;&lt; \"Elapsed \" &lt;&lt; elapsed.count() &lt;&lt; \"s\\n\";\r\n\u00a0\u00a0 \u00a0return 0;\r\n}<\/pre>\n<p>This code will call the sin() function 1 million times and measure the time it takes. We will use the time to roughly compare the performance of Raspberry Pi Zero vs. Raspberry Pi 3.<\/li>\n<li>Build the project via Ctrl-Shift-B:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2048\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-build.png\" alt=\"09-build\" width=\"924\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-build.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-build-300x210.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-build-130x90.png 130w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a><\/li>\n<li>If the C++11 features like <strong>std::chrono<\/strong> don&#8217;t work, double-check that you have C++11 selected via Project Properties:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/cxx11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2055\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/cxx11.png\" alt=\"cxx11\" width=\"822\" height=\"558\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/cxx11.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/cxx11-300x204.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>Set a breakpoint on the &#8220;return 0&#8221; line and press F5 to start debugging. Once the breakpoint is hit, check the &#8220;gdbserver&#8221; window for the output:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-time1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2056\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-time1.png\" alt=\"10-time\" width=\"924\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-time1.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-time1-300x210.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-time1-130x90.png 130w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a>In our tests running the loop on Raspberry Pi Zero took approximately 0.5 seconds.<\/li>\n<li>Now we will create another configuration to run the same program on Raspberry Pi 3. Open VisualGDB Project Properties and clone the current configuration:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-raspi-new.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2050\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-raspi-new.png\" alt=\"11-raspi-new\" width=\"813\" height=\"621\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-raspi-new.png 813w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-raspi-new-300x229.png 300w\" sizes=\"(max-width: 813px) 100vw, 813px\" \/><\/a><\/li>\n<li>Switch &#8220;deployment machine&#8221; to the regular Raspberry Pi:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-raspi3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2051\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-raspi3.png\" alt=\"12-raspi3\" width=\"813\" height=\"621\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-raspi3.png 813w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-raspi3-300x229.png 300w\" sizes=\"(max-width: 813px) 100vw, 813px\" \/><\/a><\/li>\n<li>Connect your Raspberry Pi to the network (you can use the same SD card image), wait for it to start and run the program by pressing F5. Note the new time:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/13-raspi3-speed1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2057\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/13-raspi3-speed1.png\" alt=\"13-raspi3-speed\" width=\"924\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/13-raspi3-speed1.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/13-raspi3-speed1-300x210.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/13-raspi3-speed1-130x90.png 130w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a>In our tests one core of Raspberry Pi 3 was roughly twice faster then Raspberry Pi Zero.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create C++ projects for the ultra-low-cost Raspberry Pi Zero board using Visual Studio, VisualGDB and<\/p>\n","protected":false},"author":1,"featured_media":2058,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[72,33,43],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2038"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=2038"}],"version-history":[{"count":5,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2038\/revisions"}],"predecessor-version":[{"id":2063,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2038\/revisions\/2063"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media\/2058"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=2038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=2038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=2038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}