<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gal Shelef on Medium]]></title>
        <description><![CDATA[Stories by Gal Shelef on Medium]]></description>
        <link>https://medium.com/@gal.shelef?source=rss-c5dd633c06a8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*sCXPd69aVchq9ECeQM-9Pg.jpeg</url>
            <title>Stories by Gal Shelef on Medium</title>
            <link>https://medium.com/@gal.shelef?source=rss-c5dd633c06a8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 09:44:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gal.shelef/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[So you want to write cross platform code, PAL? (Part II)]]></title>
            <link>https://medium.com/@gal.shelef/so-you-want-to-write-cross-platform-code-pal-part-ii-16afbc22c139?source=rss-c5dd633c06a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/16afbc22c139</guid>
            <dc:creator><![CDATA[Gal Shelef]]></dc:creator>
            <pubDate>Wed, 22 Jan 2020 13:32:11 GMT</pubDate>
            <atom:updated>2020-06-29T19:23:16.609Z</atom:updated>
            <content:encoded><![CDATA[<h4>Written with <a href="https://medium.com/@yair.levinson">Yair Levinson</a></h4><h3>Introduction</h3><p>In <a href="https://medium.com/@gal.shelef/so-you-want-to-write-cross-platform-code-pal-part-i-514a1fc0a315">part I</a> we discussed the architecture of applications with a cross platform C++ core, and the way to combine platform specific implementations of PAL APIs through bindings. In part II we will list several practices for defining and implementing PAL APIs, that we had learned over the course of our work on cross platform applications. Let’s dive right into the list -</p><h3>Best PAL practices</h3><p><strong>Don’t PAL what you don’t have to</strong></p><p>First and foremost, the absolutely best practice here is — don’t PAL what you already have available in C++. For example, in one cross platform project we abstracted threads. Once we could switch to C++11, we moved to the standard library’s threads and removed the abstraction.</p><p>If you can avoid writing and maintaining bindings and platform specific code, that should be an easy decision.</p><p><strong>Sometimes PAL what you don’t have to</strong></p><p>Huh? But you said… — I know. But there are exceptions to every rule, right? There are cases where you would have a cross platform alternative, but it may still make sense to PAL and write platform specific implementations.</p><p>For example, In the first part I mentioned using libcurl for networking, which we did in a certain project. We built libcurl for every platform we targeted and so we avoided writing a PAL. Then we wanted to target UWP too and couldn’t use libcurl for that because it is using APIs that are not allowed for a Windows Store app so we didn’t pass the certification kit.</p><p>We introduced PAL for networking with a simple interface suitable to our needs and implemented it on every platform. That way we could use the approved UWP networking APIs and also have access to platform specific features of networking like background fetches on iOS, or a networking thread pool provided by the OS, etc.</p><p>Going for PAL here ended up being the right choice for us in this case.</p><p><strong>Don’t if-def</strong></p><p>Don’t write your multi-platform code like this:</p><pre>#if defined(WIN32)</pre><pre>// windows stuff</pre><pre>#elif defined (MACOSX)</pre><pre>// mac stuff</pre><pre>#endif</pre><p>This kind of code is hard to maintain, because it’s hard to read. We recommend to go for readability over reuse here in most cases.</p><p>You can find an exception to this rule in <a href="http://llvm.org/docs/SupportLibrary.html#code-organization">llvm’s support library</a>. There the if-defs are only used to select the right, separate, platform specific actual implementation file.</p><p><strong>Abstract — don’t wrap</strong></p><p>Usually when writing cross platform code, you’ll start with a single platform and then later on write code to support additional platforms.</p><p>When designing a PAL interface it is tempting to define your APIs similar to the concrete implementations of that initial platform instead of designing an “abstract” enough interface which will play well with all future platforms. The lesser evil here would be to look at future supported platforms implementations and design the interface according to the greatest common denominator.</p><p>For example, suppose you want to have a PAL implementation to check if a file path exists. If your initial platform is iOS, you might tend to go and look at the iOS native implementation and define your interface to be similar.</p><p>In iOS the method is defined as follows:</p><pre>[NSFileManager fileExistsAtPath:isDirectory]</pre><p>It expects a path to the file as well as a <strong>isDirectory</strong> argument. On Android on the other hand the method is more straightforward:</p><pre>File.exists()</pre><p>It does not require the extra `isDirectory` argument. In this case, the greatest common denominator is a method with a single file path argument, which also aligns with your API requirements.</p><p>What we consider the best practice here, however, is to design the interface to be a high level abstraction that stays objective to your core requirements.</p><p><strong>Salami your bindings</strong></p><p>Follow the guidelines described <a href="https://adishavit.github.io/2017/salami-method/">here</a> and make sure you have all of the conceptual layering of the binding code. You don’t have to have actual separation of code for each layer. Just be explicitly aware of the responsibilities of the layers you do have.</p><p><strong>Choose an appropriate PAL implementation</strong></p><p>When it comes to implementing the PAL interfaces, you have a few options. There’s no right or wrong here, but it really depends on your needs, personal preferences and coding style.</p><p>Compile-time Linking — You can choose to have all your PAL implementations located in separate files and have your build system compile &amp; link them depending on the build target. For example, if you’re building iOS and Android, you’ll compile &amp; link the implementation files located under <strong>&lt;project&gt;/PAL/iOS/&lt;source files&gt;</strong> and <strong>&lt;project&gt;/PAL/Android/&lt;source files&gt;</strong>, respectively. The implementation files can have the same names for all platforms.</p><p>Runtime Linking — Your cross platform code may treat the PAL implementations as interfaces, and implementations will be injected at runtime. In this case anyone consuming the common library may have his independent PAL implementations. The PAL is not coupled with the cross platform code.</p><p>Make sure that the way you do choose fits your project in a maintainable and scalable manner, and that your conventions around how this mechanism works and how code is organized are clear and consistent throughout the project.</p><p><strong>Test on all targeted platforms</strong></p><p>We recommend that you run your tests on all the platforms you target. As we mentioned, C++ will compile on all platforms, but you still need to be aware of each platform’s specific behavior, which may make the same code act a bit differently on different platforms. Having your tests run on all the targeted platforms will help you do that.</p><p>Writing your tests in C++ will enable you to write your tests once, and run them on all the platforms you target. We use <a href="https://github.com/google/googletest">Google Test</a> as it is a solid C++ testing framework that runs on many platforms.</p><p>Every PAL interface should be covered by tests. You could write platform specific tests for every PAL implementation, and that may be suitable in certain cases. However the preferable approach would be to write the tests in C++ and execute them on every platform with its corresponding PAL implementation.</p><p>How to run gtest on different platforms is out of scope for this post, but we may publish another post dedicated to that.</p><h3>Summary</h3><p>The purpose of this post was to overview some aspects of cross platform development of applications using C++, and deep dive into one of them — the PAL. When starting off a new project, however, we wouldn’t go ahead and set up all this boilerplate right off the bat.</p><p>Some of the recommendations made here, can be held back until a later stage of the project, when they become relevant. You don’t have to Salami your bindings right away, or introduce a meta-build system, or have your tests run on all target platforms right when you start.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16afbc22c139" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[So you want to write cross platform code, PAL? (Part I)]]></title>
            <link>https://medium.com/@gal.shelef/so-you-want-to-write-cross-platform-code-pal-part-i-514a1fc0a315?source=rss-c5dd633c06a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/514a1fc0a315</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[cross-platform]]></category>
            <category><![CDATA[cpp]]></category>
            <dc:creator><![CDATA[Gal Shelef]]></dc:creator>
            <pubDate>Sun, 09 Apr 2017 13:46:39 GMT</pubDate>
            <atom:updated>2020-06-29T14:28:21.527Z</atom:updated>
            <content:encoded><![CDATA[<h4>Written with <a href="https://medium.com/u/e704388c3ceb">Yoav Schwartz</a></h4><p>Ever since mobile devices have made their appearance as an increasingly integral part of our lives, developing cross platform code has become a worthwhile investment. Multiple platforms were there long before mobile devices introduced several new operating systems, but back then, every operating system had “its thing”.</p><p><a href="https://www.youtube.com/watch?v=IYv3-HfRNcA">Mac was considered better for designers</a>, Windows was considered better for gaming and office use, and Linux was something better suited for the tech savvy. These boundaries were something that allowed developers to get away with targeting only the platform that was most appropriate for the application they were working on.</p><p>However, these boundaries are fading away as these major operating systems mature. More importantly, the new mobile operating systems don’t have the same kind of boundaries. If an app is useful on iOS then in most cases it would probably be just as useful on other mobile platforms, and the other way around.</p><h3>C++, I choose you!</h3><p>Now that you’re convinced you should aim for multiple platforms there are several ways to proceed:</p><p>You could write your app for each platform using the target platform stack (Swift/Objective-C for iOS, Java for android apps, C# for windows). You could also write your application using JavaScript (as a web application), or use a cross platform development framework like Xamarin or React.</p><p>Our approach is to write the core functionality of the app in C++, and use the platform-specific stack for the rest. Our rule of thumb to decide what should be C++ is basically everything except the GUI of your application and what can’t be cross platform and you still need (we will give a few examples).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/444/1*l-kkthuFAEDSl3Xa5eDHqw.gif" /></figure><p>We could argue when and where using C/C++ is the most appropriate way to write cross platform code or not, but we’ll leave that outside the scope of this article. Let us just establish that using C/C++ is a working, proven way to do so.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/234/1*BqiHO8va-NK5ztyn5CJTYA.png" /><figcaption>Platform is red, cross platform is blue. How do I connect between the two?</figcaption></figure><p><strong>Bindings</strong></p><p>C++ is accessible on all of the platforms we mentioned above. More accurately, C is. To cross the dreaded language boundary you must write what is known as ‘Bindings’. For some platforms, like Android, this means wrapping your C++ public API with a C API and then adding some JNI for good measure. On others like iOS and UWP, you could consume C++ through their own hybrid “binding” language.</p><p><em>However, if you plan to deliver your cross platform code as a binary, you might want to have a C API wrapper all the same for sake of </em><a href="http://rlc.vlinder.ca/blog/2009/08/binary-compatibility/"><em>ABI compatibility</em></a><em>.</em></p><p>Sounds like a lot of work? Well, we are talking long term maintainability, for a non-trivial code base here. And besides, some of these bindings can be automatically <a href="http://www.swig.org/">generated</a>!</p><p><em>A colleague of mine recently posted a great article, describing what he named “</em><a href="https://adishavit.github.io/2017/salami-method/"><em>The Salami Method</em></a><em>”. This thorough description of how to architect and organize your cross platform code was named so, because of the several (conceptual) layers that you would be wise to separate your code into. A recommended read for those who plan to write bindings for more than some foos and bars.</em></p><p><strong>You still have to be aware</strong></p><p>C++ is a cross platform programming language, and you can write bindings and call into it from multiple platforms, which is nice. There are compilers available that compile C++ code to run on various operating systems and CPU architectures.</p><p>Unfortunately, this does not solve all of our problems. You still have to be aware of the differences in the behavior of your code in the different environments it will run in. It is still much better than writing your code multiple times, but you can’t ignore the target platforms.</p><p>For example, most mobile operating systems run your app in a “sandbox” to prevent it from messing up the file system for everyone else. Your app can only access a specific area in the device file system. Windows desktop applications, however, don’t have this limitations and can access the entire file system.</p><p>That is why we recommend writing tests and running them on all the platforms you are targeting, but more on that later.</p><p><strong>But wait, there’s more</strong></p><p>Besides being aware of your platforms’ surrounding, another challenge is how to utilize functionalities that are exposed by the platforms frameworks. For example, accessing the GPS (for mobile), is not cross platform. All mobile operating systems have GPS functionality exposed by their corresponding programming language. For iOS you would have to write Objective-C or Swift to access the GPS, and for Android you would have to write Java.</p><p>The bindings would allow us to callback from our core code back to the platform, and invoke whatever we need there. But what would be a good way to design our core code around these platform invocations?</p><p>Our goal is to have our code written in such a way that would allow us to write portable, high level code, without having to be aware of which platform we’re using.</p><h3>PAL is your friend</h3><p>Move along, nothing new to see here, this problem is already solved! Whenever we want to design our code in a way that does not depend on a specific implementation we write an abstraction, right?</p><p>There may be more than one way to achieve this, but the principal is the same. Design the abstract API that you need to implement your desired functionality and let some other poor fella (probably you) worry about how to implement that API. Your side of the code stays clean and generic, and all the gory details of the unmentionable acts that had to be done to implement that API are dealt with by the platform specialist. Fantastic.</p><p>This kind of abstraction over platform-specific functionality is called a Platform Abstraction Layer, or PAL for short. We write a set of abstract interfaces that provide us with the needed platform functionalities and layer them together right at the boundary of our cross platform component with the platform:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*XXjzkDUanUXeUx8USX-qpw.png" /></figure><p>The reason there are two kinds of PAL implementations — C++ and Non C++, is that it is perfectly fine to implement PAL interface using C++. Granted, it is not the common case, because most of the time you wouldn’t have a PAL API for something that you could implement with C++ in the first place, but sometimes it makes sense.<br>For example, you might define a PAL API for networking, and provide a default implementation using libcurl (written in c). It is still a good idea to abstract that implementation away with a PAL API, to prevent tightly coupling an external dependency into your code and to keep the possibility for specific platforms to provide an implementation based on the platform’s networking library.</p><h3>Part I Summary</h3><p>Why would we go into all this trouble to explain this specific case of what seems yet another abstraction? Because this case is both becoming more common due to the growing usefulness of writing cross platform code, and it has some characteristics that we can generalize into what we feel are PAL best practices.</p><p>What kind of characteristics are we talking about? For example, the common case for PAL interfaces is that they are defined on one side of the bindings, and implemented on the other side. Another example is that there are common pitfalls when designing these PAL APIs like making the API too low level.</p><p>The next article will go over these best practices and explain them. In <a href="https://medium.com/@gal.shelef/so-you-want-to-write-cross-platform-code-pal-part-ii-16afbc22c139">part II</a> we will also dive deeper into how to test cross platform code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=514a1fc0a315" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using libcurl in Universal Windows Platform]]></title>
            <link>https://medium.com/@gal.shelef/building-libcurl-for-universal-windows-platform-9c8d4f2f7240?source=rss-c5dd633c06a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c8d4f2f7240</guid>
            <category><![CDATA[uwp]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[windows]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[curl]]></category>
            <dc:creator><![CDATA[Gal Shelef]]></dc:creator>
            <pubDate>Tue, 17 Jan 2017 12:30:38 GMT</pubDate>
            <atom:updated>2017-10-08T07:32:26.755Z</atom:updated>
            <content:encoded><![CDATA[<p>Networking, and specifically the execution of http requests, is a very useful functionality nowadays. A great deal of the applications we see have some kind of a backend that they need to communicate with for all sorts of reasons. Therefore, every platform provides extensive networking APIs as part of its SDK and writing a piece of code that issues an http request is not a difficult exercise.</p><p>However, when aiming to write code that is cross platform, things get a bit more complicated (as they usually do!). All of the latest projects I worked on targeted multiple platforms, and required executing http requests. We wrote the cross platform parts of the code using C and C++, and then it was left to decide whether to use a third party networking C / C++ library like curl, or to write an abstraction layer (PAL) with networking interfaces, and implement them on each platform.</p><p>On one project, we chose to do the former and on the other, the latter. As there are many things to consider when deciding whether to introduce a PAL or not, I will keep it outside the scope of this post. This could be the subject of a separate <a href="https://medium.com/@gal.shelef/so-you-want-to-write-cross-platform-code-pal-part-i-514a1fc0a315">blog post</a>.</p><p>In this post I will dive into the technical details of a project where we use libcurl and explain how I built libcurl for Windows Universal Platform (UWP) for x64 architecture.</p><p>Officially libcurl is distributed as source packages, but some external contributors provide binary packages for several platforms. You can find the various available binary distributions at <a href="https://curl.haxx.se/download.html">https://curl.haxx.se/download.html</a>. There are also other distributions people have created, like this one here <a href="https://github.com/gcesarmza/curl-android-ios">https://github.com/gcesarmza/curl-android-ios</a> for iOS and Android, for example.</p><p>Following are the steps that got libcurl building and running:</p><ol><li>Get OpenSSL — WinSSL didn’t work for me (I got SSL connection error). You can get it from <a href="http://slproweb.com/products/Win32OpenSSL.html">http://slproweb.com/products/Win32OpenSSL.html</a> — install win64 version.</li><li>Get curl source code. I got it from github at <a href="https://github.com/curl/curl">https://github.com/curl/curl</a>.</li><li>Use the command line to run <em>“C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\vcvars64.bat”</em>. This might be at another location for you according to your Visual Studio version. The important thing is to run vcvars64 and not vcvars32. Otherwise you would get <em>“module machine type ‘X86’ conflicts with target machine type ‘x64’”</em> error.</li><li>cd to wherever you put curl, to the winbuild subdirectory, and run<br><em>nmake /f Makefile.vc mode=dll ENABLE_SSPI=no ENABLE_IDN=no DEBUG=no MACHINE=X64 GEN_PDB=no ENABLE_IPV6=yes VC=14 ENABLE_WINSSL=no WITH_SSL=static WITH_DEVEL=C:\OpenSSL-Win64<br></em>VC and WITH_DEVEL params may vary according to your VS version and where you installed OpenSSL</li><li>The artifacts are in the ‘builds’ subfolder under curl. Leave them or copy them to wherever. You will link to these from your project.</li><li>Open Visual Studio and create a Win Universal app.</li><li>Add an include and link search path to where you copied the curl artifacts.</li><li>To make sure your executable finds the curl and open ssl dlls, you can either add their locations to the PATH environment variable, or have them copied by Visual Studio to be beside your executable by adding the dlls to the project and right clicking each dll, selecting properties, and setting <em>Content</em> to <em>YES</em></li><li>Download cacert.pem from <a href="http://curl.haxx.se/docs/caextract.html">http://curl.haxx.se/docs/caextract.html</a>. Rename it to curl-ca-bundle.crt (based on this <a href="http://www.oracle.com/webfolder/technetwork/tutorials/obe/cloud/objectstorage/restrict_rw_accs_cntainers_REST_API/files/installing_curl_command_line_tool_on_windows.html">page</a>). Make sure curl-ca-bundle.crt can be found by your executable, so your code can find it.</li><li>curl will cause a compilation error because the universal app visual studio template defaults to including winsock.h. The way I worked around that was to have the <em>WIN32_LEAN_AND_MEAN</em> macro defined for my project. Another way that should work is to <em>#include &lt;winsock2.h&gt;</em> at the project’s pch.h.</li><li>After going through these steps, the following code worked for me. It is based on the example shown at <a href="https://curl.haxx.se/libcurl/c/http-post.html">https://curl.haxx.se/libcurl/c/http-post.html</a>:</li></ol><pre><em>CURL *curl;<br>CURLcode res;</em></pre><pre><em>/* In windows, this will init the winsock stuff */<br>curl_global_init(CURL_GLOBAL_ALL);</em></pre><pre><em>/* get a curl handle */<br>curl = curl_easy_init();</em></pre><pre><strong><em>if</em></strong><em> (curl) {<br> curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);<br> curl_easy_setopt(curl, CURLOPT_URL, “https://whatever&quot;);<br> curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);<br> curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);<br> curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1);<br> curl_easy_setopt(curl, CURLOPT_CAINFO, “curl-ca-bundle.crt”);</em></pre><pre><em> res = curl_easy_perform(curl);</em></pre><pre><em> /* always cleanup */<br> curl_easy_cleanup(curl);<br>}</em></pre><p>Hopefully, this post will help out those who find themselves in need of libcurl for UWP like I did. I would love to get feedback so let me know if you have any questions or comments on this subject.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c8d4f2f7240" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>