Fundamentals 12 min read

Unlock Cross‑Platform C Development with TBOX: A Comprehensive Library Overview

TBOX is an open‑source C library offering a unified, cross‑platform API that simplifies common development tasks across Windows, macOS, Linux, Android, iOS and BSD, featuring modules for streams, coroutines, memory, containers, algorithms, networking, databases, and more, with build instructions via xmake.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Unlock Cross‑Platform C Development with TBOX: A Comprehensive Library Overview

TBOX Overview

TBOX is an open‑source C library that provides a unified, cross‑platform development interface, simplifying routine operations and allowing developers to focus on application logic.

Supported Platforms

Windows, macOS, Linux, Android, iOS, and various BSD variants are all supported.

Build Integration

The library is built with the xmake build system and offers four compilation modes:

Release : disables debugging information and assertions, enables compiler optimizations.

Debug : enables detailed debug info, assertions, memory overrun detection, leak detection, and lock‑contention analysis.

Small : minimal build that disables all optional modules and applies size‑optimizing compiler flags.

Micro : targets embedded platforms, compiling only the core micro‑kernel (≈64 KB) with a lightweight libc implementation.

Key Modules

Stream Library : unified I/O for HTTP, file, socket, and data streams; supports blocking, non‑blocking, and asynchronous modes; allows insertion of multiple filter layers for on‑the‑fly decompression, encoding conversion, encryption, etc.

Coroutine Library : fast, efficient coroutine switching based on a Boost‑inspired algorithm; supports stackful and stackless modes; provides channels, semaphores, locks, and native coroutine support for sockets, streams, pipes, and processes; includes example HTTP/file servers that run with only a few hundred lines of code.

Memory Library : Linux‑kernel‑inspired memory‑pool architecture; detects leaks, overflows, and overlaps in debug mode; optimizes allocation for large, small, and string data, achieving O(1) allocation in most cases.

Container Library : hash tables, linked lists, arrays, queues, stacks, min/max heaps; all containers support iterators and stream‑based serialization/deserialization.

Algorithm Library : common sorting (bubble, heap, quick, insertion), searching (linear, binary), traversal, deletion, statistics; designed with iterator‑based interfaces similar to the C++ STL but implemented in C for lightweight usage.

Network Library : HTTP client, cookie handling, DNS resolution with caching, SSL support (OpenSSL, PolarSSL, mbedTLS), IPv4/IPv6, and coroutine‑based asynchronous I/O; integrates with epoll, kqueue, poll, select, and IOCP.

Database Library : unified database interface using URL‑based connection strings; supports SQLite3 and MySQL out of the box, with extensibility for other relational databases.

XML Library : DOM and SAX parsing modes; SAX uses an external iterator for high performance; supports streaming parsing, incremental decompression, and on‑the‑fly transformation; includes an XML writer.

Math Library : fixed‑point arithmetic of various precisions and a random‑number generator.

libc Library : lightweight, cross‑platform C standard library implementation; provides extensive string and wide‑string operations, case‑insensitive utilities, and optimized memory functions such as memset_u16 and memset_u32.

libm Library : partial lightweight implementation of common math functions; adds integer versions of sqrt, log2, etc., avoiding floating‑point overhead for embedded use.

Object Library : CoreFoundation‑style object system with objects, dictionaries, arrays, strings, numbers, dates, and data; supports serialization to XML, JSON, binary, and Apple plist formats; binary format includes simple encryption and reduces size by ~30 % compared to bplist.

Platform Library : wrappers for file, directory, socket, thread, time, atomic operations, high‑precision timers, thread pools, synchronization primitives (event, mutex, semaphore, spinlock), dynamic library loading, and cross‑platform I/O pollers.

Compression Library : zlib/gzip compression and decompression (requires external zlib).

Character Encoding Library : conversion among UTF‑8, UTF‑16, GBK, GB2312, UCS‑2, UCS‑4 with endianness handling.

Utility Library : base64/32 encoding, common hash algorithms (crc32, adler32, md5, sha1), logging, assertions, URL encoding/decoding, bit‑level parsing and swapping, singleton pattern, command‑line option parsing.

Regular Expression Library : matching and replacement with global, multiline, case‑insensitive modes; built on PCRE, PCRE2, and POSIX regex engines.

Example Projects Using TBOX

gbox – https://github.com/tboox/gbox

vm86 – https://github.com/tboox/vm86

xmake – https://www.xmake.io/cn

itrace – https://github.com/tboox/itrace

… (additional projects omitted for brevity)

Build Instructions

Install xmake (https://github.com/xmake-io/xmake) and run the following commands inside the tbox directory:

# Build for the host platform
cd ./tbox
xmake

# Build for MinGW
cd ./tbox
xmake f -p mingw --sdk=/home/mingwsdk
xmake

# Build for iPhoneOS
cd ./tbox
xmake f -p iphoneos
xmake

# Build for Android
cd ./tbox
xmake f -p android --ndk=xxxxx
xmake

# Cross‑compile for Linux with a custom SDK
cd ./tbox
xmake f -p linux --sdk=/home/sdk
xmake

Sample Code

#include 'tbox/tbox.h'

int main(int argc, char** argv)
{
    // initialise TBOX
    if (!tb_init(tb_null, tb_null)) return 0;

    tb_trace_i('hello tbox');

    // vector example
    tb_vector_ref_t vector = tb_vector_init(0, tb_element_str(tb_true));
    if (vector)
    {
        tb_vector_insert_tail(vector, 'hello');
        tb_vector_insert_tail(vector, 'tbox');
        tb_for_all(tb_char_t const*, cstr, vector)
        {
            tb_trace_i('%s', cstr);
        }
        tb_vector_exit(vector);
    }

    // stream example (HTTP GET)
    tb_stream_ref_t stream = tb_stream_init_from_url('http://www.xxx.com/file.txt');
    if (stream && tb_stream_open(stream))
    {
        tb_long_t size = 0;
        tb_char_t line[TB_STREAM_BLOCK_MAXN];
        while ((size = tb_stream_bread_line(stream, line, sizeof(line))) >= 0)
        {
            tb_trace_i('line: %s', line);
        }
        tb_stream_exit(stream);
    }

    tb_getchar(); // pause
    tb_exit();   // cleanup TBOX
    return 0;
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

cross-platformCopen sourceLibrarySystems Programmingxmake
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.