<?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"><channel><title><![CDATA[tqjxlm's Tech Blog]]></title><description><![CDATA[tqjxlm's Tech Blog]]></description><link>https://tqjxlm.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 12 Oct 2026 17:34:59 GMT</lastBuildDate><atom:link href="https://tqjxlm.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Cross-Platform Mobile C++ Development with CMake: A Unified Build System for iOS, Android, Windows, and macOS]]></title><description><![CDATA[Introduction
Have you ever dreamed of writing C++ code once and deploying it seamlessly across iOS, Android, Windows, and macOS? I recently embarked on this journey while developing a mobile hardware ray tracing renderer. The goal was ambitious: crea...]]></description><link>https://tqjxlm.hashnode.dev/cross-platform-mobile-c-development-with-cmake-a-unified-build-system-for-ios-android-windows-and-macos</link><guid isPermaLink="true">https://tqjxlm.hashnode.dev/cross-platform-mobile-c-development-with-cmake-a-unified-build-system-for-ios-android-windows-and-macos</guid><category><![CDATA[C++]]></category><category><![CDATA[CMake]]></category><category><![CDATA[Mobile Development]]></category><category><![CDATA[Rendering]]></category><dc:creator><![CDATA[tqjxlm]]></dc:creator><pubDate>Sun, 29 Jun 2025 09:36:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1751153381470/bbeb7e7c-a071-424d-97f9-da9673b040c8.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>Have you ever dreamed of writing C++ code once and deploying it seamlessly across iOS, Android, Windows, and macOS? I recently embarked on this journey while developing a mobile hardware ray tracing renderer. The goal was ambitious: create a framework that rivals the convenience of modern web development tools, but for C++ applications targeting all major platforms.</p>
<p>The good news? In 2025, this is not only possible but surprisingly achievable. I've open-sourced the result as <strong><a target="_blank" href="https://github.com/tqjxlm/Sparkle">Sparkle: A Cross-Platform Hardware Ray Tracing Renderer</a></strong>.</p>
<h2 id="heading-why-now">Why Now?</h2>
<p>The C++ ecosystem has evolved significantly:</p>
<ol>
<li><strong>Modern C++</strong> - The standard library is richer than ever, and tools like <a target="_blank" href="https://vcpkg.io/">vcpkg</a> have revolutionized package management</li>
<li><strong>Universal Clang Support</strong> - All platforms now support Clang, with Android and macOS using it as their default compiler</li>
<li><strong>Mature CMake</strong> - While still quirky, CMake has become the de facto standard for cross-platform C++ builds</li>
<li><strong>Platform Evolution</strong> - Each platform has developed mature toolchains that still embrace C++</li>
<li><strong>Graphics API Convergence</strong> - Vulkan's cross-platform design and the convergence of graphics APIs make unified rendering possible</li>
<li><strong>Python Everywhere</strong> - Python's ubiquity provides a "free" cross-platform scripting solution for complex build automation</li>
<li><strong>AI Assistance</strong> - Modern AI tools can help navigate documentation, automate tedious tasks, and debug complex build issues</li>
</ol>
<h2 id="heading-the-magic-one-command-any-platform">The Magic: One Command, Any Platform</h2>
<p>Here's what the final build system looks like:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># Build for Windows (on Windows)</span>
python3 build.py --framework glfw --config Release

<span class="hljs-comment"># Build and run on macOS</span>
python3 build.py --framework glfw --run

<span class="hljs-comment"># Generate Visual Studio project (Windows)</span>
python3 build.py --framework glfw --generate_only

<span class="hljs-comment"># Build Android APK and deploy to connected device</span>
python3 build.py --framework android --run

<span class="hljs-comment"># Build for iOS with hardware ray tracing</span>
python3 build.py --framework ios --config Release --run --pipeline gpu
</code></pre>
<p>The result? Identical rendering output across all platforms:</p>
<p><img src="https://pic2.zhimg.com/v2-f97b617c2a15ed30600b2afd664788a1_1440w.jpg" alt="Cross-platform rendering result" /></p>
<h2 id="heading-design-philosophy">Design Philosophy</h2>
<h3 id="heading-core-principles">Core Principles</h3>
<ol>
<li><strong>Platform-Agnostic by Default</strong> - Use standard library and cross-platform libraries wherever possible</li>
<li><strong>Isolate Platform-Specific Code</strong> - Keep platform dependencies contained and minimal</li>
<li><strong>Minimize Macros</strong> - Reserve them only for compiler/platform-specific needs</li>
<li><strong>Source Dependencies Over Binaries</strong> - Increases build time but dramatically reduces maintenance overhead</li>
<li><strong>Python Over CMake/C++</strong> - Use Python for complex build logic instead of CMake scripting</li>
<li><strong>Modern Language Features</strong> - Embrace the latest C++ standards and compiler features</li>
<li><strong>Compiler as Guardian</strong> - Enable all warnings, treat warnings as errors, enforce code style</li>
<li><strong>Unified Toolchain</strong> - Standardize on Clang across platforms while maintaining native toolchain compatibility</li>
<li><strong>Configuration Over Convention</strong> - Make features toggleable for easier debugging and experimentation</li>
</ol>
<h3 id="heading-strategic-trade-offs">Strategic Trade-offs</h3>
<ol>
<li><strong>Latest Everything</strong> - Support only the newest toolchains, SDKs, and hardware to leverage cutting-edge features</li>
<li><strong>Focused Feature Set</strong> - Implement representative, cross-platform solutions rather than comprehensive feature coverage</li>
<li><strong>Delayed Optimization</strong> - Prioritize stability and maintainability over premature optimization</li>
</ol>
<h2 id="heading-architecture-overview">Architecture Overview</h2>
<h3 id="heading-module-structure">Module Structure</h3>
<pre><code>sparkle/
├── libraries/          # Platform-independent modules
│   ├── core/          # Logging, math, threading, profiling
│   ├── io/            # Resource loading and file systems
│   ├── scene/         # Scene graph and nodes
│   ├── renderer/      # Rendering pipeline
│   ├── rhi/           # Graphics API abstraction
│   └── application/   # Main loop and initialization
├── frameworks/        # Platform-specific implementations
│   ├── glfw/         # Desktop (Windows/macOS)
│   ├── android/      # Android-specific code
│   ├── apple/        # Shared Apple platform code
│   ├── macos/        # macOS native support
│   └── ios/          # iOS native support
├── shaders/          # Cross-platform shaders
├── resources/        # Assets and configurations
├── build_system/     # Build scripts and platform resources
└── thirdparty/       # Dependencies
</code></pre><h3 id="heading-cmake-architecture">CMake Architecture</h3>
<p>The build system uses three strategically designed CMake files:</p>
<ol>
<li><strong>Main CMake</strong> - Manages global parameters, creates core libraries, handles platform-specific configurations</li>
<li><strong>Dependencies CMake</strong> - Consolidates all third-party dependencies into a single static library</li>
<li><strong>Shader CMake</strong> - Manages shader compilation (likely to be replaced with Python/C++ in the future)</li>
</ol>
<h3 id="heading-key-dependencies">Key Dependencies</h3>
<ul>
<li><strong>GLFW</strong> - Mature cross-platform windowing for desktop platforms</li>
<li><strong>Eigen</strong> - Powerful math library (overkill for graphics, but incredibly robust)</li>
<li><strong>Dear ImGui</strong> - Lightweight, flexible immediate-mode GUI</li>
<li><strong>spdlog</strong> - Fast, feature-rich logging</li>
<li><strong>cpptrace</strong> - Cross-platform stack traces for debugging</li>
</ul>
<h2 id="heading-platform-implementation-details">Platform Implementation Details</h2>
<h3 id="heading-windows-the-reference-implementation">Windows: The Reference Implementation</h3>
<p>Windows development is straightforward thanks to GLFW and Microsoft's legendary backward compatibility. The interesting part is <strong>Clang on Windows</strong> support:</p>
<ul>
<li>Install LLVM toolchain via Visual Studio Installer (clang-cl)</li>
<li>Develop without touching Visual Studio IDE</li>
<li>Unified compiler behavior across all platforms</li>
<li>Note: VS ships Clang 16 while official Clang is at 18+</li>
</ul>
<h3 id="heading-macos-native-metal-support">macOS: Native Metal Support</h3>
<p>Sparkle provides two macOS implementations:</p>
<ol>
<li><strong>GLFW</strong> - Works out of the box with MoltenVK for Vulkan translation</li>
<li><strong>Native</strong> - Required for Metal ray tracing support</li>
</ol>
<p>The native implementation bridges C++ and Apple's frameworks:</p>
<pre><code class="lang-objc"><span class="hljs-comment">// Key components connected via Storyboard</span>
- AppDelegate    → Application lifecycle
- MetalView      → Rendering context and swapchain
- ViewController → Window management and events
- Storyboard     → Wires everything together
</code></pre>
<h3 id="heading-ios-mobile-challenges">iOS: Mobile Challenges</h3>
<p>iOS support required several adaptations:</p>
<ol>
<li><strong>Build System</strong> - Using <a target="_blank" href="https://github.com/leetal/ios-cmake">ios-cmake</a> for proper toolchain configuration</li>
<li><strong>Code Signing</strong> - Automatic signing for local development (no paid developer account required)</li>
<li><strong>Touch Events</strong> - Custom UIKit integration for touch handling</li>
<li><strong>ImGui Backend</strong> - Custom implementation for iOS touch and rendering</li>
<li><strong>File System</strong> - Sandbox-aware file handling using NSBundle and app directories</li>
</ol>
<h3 id="heading-android-gameactivity-integration">Android: GameActivity Integration</h3>
<p>Android's approach uses GameActivity for C++ integration:</p>
<ol>
<li>Create a Kotlin/Java class extending GameActivity</li>
<li>Load the C++ library from this class</li>
<li>Implement GameActivity hooks in C++</li>
<li>Configure AndroidManifest.xml to use your GameActivity</li>
<li>Link everything through gradle's externalNativeBuild</li>
</ol>
<h2 id="heading-the-build-system-magic">The Build System Magic</h2>
<p>The Python-based build system provides consistent commands across all platforms:</p>
<h3 id="heading-features">Features</h3>
<ul>
<li><strong>Platform Selection</strong> - Target any supported platform</li>
<li><strong>Build Configurations</strong> - Debug/Release modes</li>
<li><strong>Instant Run</strong> - Build and deploy in one command</li>
<li><strong>Mobile Deployment</strong> - Direct USB deployment to devices</li>
<li><strong>IDE Integration</strong> - Generate native project files</li>
<li><strong>LSP Support</strong> - Export compile_commands.json for clangd</li>
<li><strong>Advanced Options</strong> - Shader debugging, ASAN, profiling</li>
</ul>
<h3 id="heading-automatic-dependency-management">Automatic Dependency Management</h3>
<p>The build system handles most dependencies automatically:</p>
<ol>
<li><strong>Git Submodules</strong> - Checked and updated on each build</li>
<li><strong>CMake FetchContent</strong> - Downloaded during configuration</li>
<li><strong>Vulkan SDK</strong> - Silent installation to project directory</li>
<li><strong>Binary Dependencies</strong> - Via system package manager or vcpkg fallback</li>
<li><strong>macOS Tools</strong> - LLVM and CMake via Homebrew</li>
</ol>
<p>Manual installation is still required for:</p>
<ul>
<li>Visual Studio</li>
<li>Xcode</li>
<li>Android Studio</li>
<li>Platform-specific SDKs</li>
</ul>
<h2 id="heading-tested-platforms">Tested Platforms</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Platform</td><td>OS</td><td>CPU</td><td>GPU</td></tr>
</thead>
<tbody>
<tr>
<td>Windows</td><td>Windows 11</td><td>Ryzen 5975WX</td><td>GeForce RTX 4080</td></tr>
<tr>
<td>Android</td><td>Android 13</td><td>Snapdragon 8 Gen2</td><td>Adreno 740</td></tr>
<tr>
<td>macOS</td><td>macOS 15.5</td><td>Apple M3 Pro</td><td>Apple M3 Pro</td></tr>
<tr>
<td>iOS</td><td>iOS 18.5</td><td>Apple A18</td><td>Apple A18</td></tr>
</tbody>
</table>
</div><h2 id="heading-conclusion">Conclusion</h2>
<p>Building a truly cross-platform C++ application framework is challenging but achievable. The key is embracing modern tools, making strategic trade-offs, and maintaining a clear separation between platform-agnostic and platform-specific code.</p>
<p>Sparkle demonstrates that with careful design, you can achieve the "write once, run everywhere" dream for C++ applications, even with advanced features like hardware ray tracing.</p>
<p>The project is actively developed and welcomes contributions. Whether you're building a game engine, a rendering system, or any cross-platform C++ application, I hope Sparkle's approach provides valuable insights for your journey.</p>
<p>Check out the full source code and documentation at <strong><a target="_blank" href="https://github.com/tqjxlm/Sparkle">github.com/tqjxlm/Sparkle</a></strong>.</p>
<hr />
<p><em>Have you tackled cross-platform C++ development? What challenges did you face? Share your experiences in the comments below!</em></p>
]]></content:encoded></item></channel></rss>