{"id":238,"date":"2008-02-16T22:05:28","date_gmt":"2008-02-16T22:05:28","guid":{"rendered":"http:\/\/dalelane.co.uk\/blog\/?p=238"},"modified":"2008-02-17T13:48:24","modified_gmt":"2008-02-17T13:48:24","slug":"building-c-code-for-windows-mobile","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=238","title":{"rendered":"Building C code for Windows Mobile"},"content":{"rendered":"<p>I was asked to get a C application working on Windows Mobile. Some colleagues at work have got some hobby code : a bunch of C files which they want to try out on a huge range of mobile platforms such as a <a href=\"http:\/\/www.nslu2-linux.org\/\">slug<\/a>. On Linux-based platforms, it&#8217;s apparently fairly straightforward &#8211; without too much work, <a href=\"http:\/\/gcc.gnu.org\/\">gcc<\/a> or some such compiler turns the code into a neat little executable. <\/p>\n<p>Now the plan is to try it on a Windows Mobile device. And, probably cos I <a href=\"http:\/\/dalelane.co.uk\/blog\/?s=windows%20mobile\">won&#8217;t shut up about Windows Mobile<\/a>, they asked if I&#8217;d fancy giving the code a try on Windows Mobile. <\/p>\n<p><!--more-->What were my options?<\/p>\n<p><strong>a) Rewrite the app in one of the languages supported on Windows Mobile &#8211; C++ or C#<\/strong><\/p>\n<p>As hobby code in an early stage of development, it&#8217;s changing a lot. I was reluctant to invest much effort in forking the code at this point to write a version of the app in C++ or C#. <\/p>\n<p><strong>b) Try and get the C code to compile for Windows Mobile<\/strong><\/p>\n<p>This way I can keep changes to the existing code-base to a minimum. And I thought it&#8217;d be quicker. <\/p>\n<p>So I went with <strong>(b)<\/strong>. <\/p>\n<p>It was an interesting bit of work, and not something I&#8217;ve tried before. <\/p>\n<p>The SDK for Windows Mobile has a compiler for Visual C++. And C++ is a superset of C, so I should have been fine, right?<\/p>\n<p>Well, no. <\/p>\n<p>Yes, I have a compiler that can compile C code &#8211; and if I had code with just the basic bits and primitives of C, I&#8217;d be pretty much fine. <\/p>\n<p>What I&#8217;m missing is big chunks of the CRT (C Run-Time Library). Presumably because C isn&#8217;t a supported development environment for Windows Mobile (<em>Update: see comments below<\/em>), and this is a mobile platform where resources are scarce. If you don&#8217;t want developers to be writing C, why bother filling the storage of a mobile device with all that CRT goodness?<\/p>\n<p>When I imported the C code into Visual Studio, and configured it for a Windows Mobile target, it came up with hundreds of errors. And nearly all of them were due to functions used in the C code which were missing in what passes for the CRT I&#8217;ve got! <\/p>\n<p>So the way to get C code to build for Windows Mobile seems to be to write replacements for the C functions used by the original developer, but missing from the mobile CRT. Which meant I spent a fun couple of evenings reading man pages for C functions in the Linux Programmer&#8217;s Manual, and trying to cobble together my own versions that conformed to the UNIX versions that the original developer was coding to. <\/p>\n<p>If I recall correctly, the first one was <a target=\"_blank\" href=\"http:\/\/www.linuxhowtos.org\/manpages\/3p\/strtok_r.htm\"><code>strtok_r<\/code><\/a> and I went from there.<\/p>\n<p>Interestingly, even after I got the code to compile, it then spat out a bunch of linker errors. It turns out that the Visual Studio SDK has <a href=\"http:\/\/connect.microsoft.com\/VisualStudio\/feedback\/ViewFeedback.aspx?FeedbackID=117483\">a bug where the time functions were mistakenly left in the headers<\/a> for Windows Mobile, even though the actual implementations were all removed. So the code &#8211; which includes a bunch of uses of C time functions &#8211; compiled fine, but then failed to link because the implementations were missing. So I had to back to writing my own implementations!<\/p>\n<p>Not sure that I&#8217;d recommend this as a sensible long-term approach, but as a quick hack to get some code working, it was an interesting exercise. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was asked to get a C application working on Windows Mobile. Some colleagues at work have got some hobby code : a bunch of C files which they want to try out on a huge range of mobile platforms such as a slug. On Linux-based platforms, it&#8217;s apparently fairly straightforward &#8211; without too much [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[129,36,571,128,22,19],"class_list":["post-238","post","type-post","status-publish","format-standard","hentry","category-code","tag-build","tag-c","tag-code","tag-compile","tag-visual-studio","tag-windows-mobile"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=238"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}