☠️
smadi0x86 Playground
  • 💀Welcome to smadi0x86 Playground
    • 🍷Resources
    • 🚬Projects
    • 🎓Certifications
    • 📌Pinned
    • ❓Questions
    • 📞Contact
  • 🏞️Cloud Native
    • Docker
      • Quick Reference
      • Introduction
      • Containers
      • Images
      • Storage & Volumes
      • Security
      • Cheatsheet
    • Git
    • Serverless Framework
    • YAML
  • 🔨Software Engineering
    • System Design
    • Environment Variables
    • JSON Web Tokens
  • 👾Architecture
    • C Language
      • Introduction
      • Calling Conventions
      • GCC Compilation
      • Libraries & Linking
      • I/O
      • Files
      • Pointers
      • Dynamic Memory Allocation
      • Data Types
      • Strings Manipulation
      • Bit Manipulation
      • Pre-processors
      • Macros
      • Type Qualifiers
    • C/C++ Build Systems
      • Fundamentals for Linking
      • Symbolic Linking
      • Cross-Platform Compilation
      • CMake for Building and Linking
      • Shared Libraries
      • Dynamic Linking and Dependency Management
    • Operating Systems
      • OS & Architecture
      • Processes
      • CPU Scheduling
      • Memory Management
  • 🛩️Cyber Warfare
    • Flight Physics
    • Communication
      • PWM & PPM
      • MAVLink
  • 🏴‍☠️Offensive Security
    • Active Directory
      • Introduction
    • Web Attacks
      • Server Side
        • OS Command Injection
        • Information Disclosure
        • Directory Traversal
        • Business Logic
        • Authentication
        • File Upload
        • SSRF
      • Client Side
        • CSRF
        • XSS
    • Recon
      • Active
        • Host discovery
        • Nmap
        • Mass Scan
      • Passive
        • Metadata
      • Web Applications
        • Discovery
        • Subdomains & Directories
        • SSL Certs
        • CMS
        • WAF Detection
      • Firewall Evasion
  • Binary Exploitation
    • Stack Smashing
      • x86
      • x86_64
    • pwntools
      • Processes and Communication
      • Logging and Context
      • Cyclic
      • Packing
      • ELF
      • ROP
  • 😈Advanced Persistent Threat
    • C2
      • Sliver
    • Malware
      • Windows Internals
        • PEB
      • Academy
        • Basics
      • Sektor7
        • Essentials
  • 💌Certifications
    • AWS Certified Cloud Practitioner (CLF-C01)
      • Cloud Foundations
      • Domain 1: Cloud Concepts
      • Domain 2: Security and Compliance
      • Domain 3: Technology
      • Domain 4: Billing and Pricing
    • AWS Certified Solutions Architect - Associate (SAA-C03)
      • Foundation
    • Certified Kubernetes Administrator (CKA)
      • Core Concepts
      • Scheduling
      • Logging & Monitoring
      • Application Lifecycle Management
      • Cluster Maintenance
      • Security
      • Storage
      • Networking
      • Design Kubernetes Cluster
      • Kubernetes The Kubeadm Way
      • Troubleshooting
      • JSONPATH
      • Lightning Lab
      • Mock Exams
      • Killer Shell
    • Certified Kubernetes Security (CKS)
      • Foundation
      • Cluster Setup
      • Cluster Hardening
      • Supply Chain Security
      • Runtime Security
      • System Hardening
      • Killer Shell
    • (KGAC-101) Kong Gateway Foundations
      • Introduction to APIs and API Management
      • Introduction to Kong Gateway
      • Getting Started with Kong Enterprise
      • Getting Started with Kong Konnect
      • Introduction to Kong Plugins
  • 📜Blog Posts
    • Modern Solutions For Preventing Ransomware Attacks
Powered by GitBook
On this page
  • The Challenge
  • Strategies for Cross-Platform
  • Building a Simple Cross-Platform Application
  1. Architecture
  2. C/C++ Build Systems

Cross-Platform Compilation

Cross-platform compilation empowers you to build C/C++ applications that run on different operating systems without rewriting the entire codebase.

The Challenge

  • Compilers and linkers on different platforms have variations in syntax, standard libraries, and available functions. Code written for one platform might not compile or run correctly on another.

  • Cross-platform compilation tackles this challenge by building the program for a target platform from a single codebase running on a development machine.

Strategies for Cross-Platform

Conditional Compilation:

  • Employ #ifdef, #ifndef, #else, and #endif directives to conditionally include code blocks based on pre-defined macros. You can define these macros based on the target platform during the build process.

#ifdef WINDOWS
#include <windows.h>
#else
#include <unistd.h>
#endif

// Code using platform-specific headers

Compiler and Linker Flags:

  • Different platforms might require specific compiler and linker flags for optimization or compatibility. CMake allows setting these flags based on the target platform.

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
  target_compile_features(MyProgram PRIVATE cxx11)  # Enable C++11 for Linux
endif()

if(CMAKE_SYSTEM_NAME MATCHES "Windows")
  target_link_libraries(MyProgram PRIVATE user32 gdi32)  # Link with Windows UI libraries
endif()

CMake supports defining toolchain files that specify the compiler, linker, and their flags for various platforms. This provides a centralized location for platform-specific configurations.

CMake excels at streamlining cross-platform builds. It offers features like:

  • Platform Detection: CMake can identify the current development platform.

  • Conditional Statements: Use CMake's if, else, and endif statements to manage platform-specific configurations.

  • Target Properties: Set compiler and linker flags for different platforms using target properties.

  • Toolchain Files: Create reusable toolchain files for various platforms.

Building a Simple Cross-Platform Application

Scenario:

You're developing a text processing application that needs to access the file system. You want to build it for both Linux and Windows.

The CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)

project(MyTextProcessor)

# Define a function to access the file system (implementation details omitted)
function(access_file FILENAME)
  if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    # Use POSIX functions
    message("Using POSIX functions for file access")
  else()
    # Use Windows API functions
    message("Using Windows API functions for file access")
  endif()
  # Implement file access logic based on the platform
endfunction(access_file)

# Source files
add_executable(MyTextProcessor main.cpp)

# Call the access_file function during compilation
add_custom_command(TARGET MyTextProcessor PRE_BUILD
  COMMAND access_file
  COMMENT "Determine file access method based on platform"
)

Explanation:

  • The access_file function uses conditional statements to determine the platform and choose the appropriate file access method (not shown here for brevity).

  • A custom command is added to call the access_file function before building the executable. This ensures the platform-specific code is included based on the detected platform.

PreviousSymbolic LinkingNextCMake for Building and Linking
👾