Discover TBOX: A Powerful Cross‑Platform C Development Library
TBOX is a C‑based cross‑platform development library that unifies interfaces for multiple operating systems, offers a rich set of modules such as streams, coroutines, databases, networking, and utilities, and provides detailed build instructions and example code for rapid integration.
TBOX Overview
TBOX is a cross‑platform development library written in C. It abstracts platform differences so developers can focus on application logic while still benefiting from each platform's optimizations.
Supported Platforms
Windows, macOS, Linux, Android, iOS, *BSD and other Unix‑like systems.
Build Modes (via xmake)
Release : Production build with debugging disabled, assertions off, and compiler optimizations enabled.
Debug : Enables detailed debug information, assertions, memory overrun detection, memory leak detection, and lock‑contention analysis.
Small : Minimal build that disables optional modules and applies minimal compiler optimizations.
Micro : Embedded‑focused build that compiles only the TBOX micro‑kernel (≈64 KB) with essential cross‑platform interfaces.
Key Features
Stream Library
stream: General non‑blocking stream supporting coroutine‑based asynchronous transfer. transfer: Manages bidirectional stream transmission. static_stream: Optimized static stream for lightweight, fast data parsing.
Coroutine Library
Fast, efficient coroutine switching; core algorithm inspired by Boost.
Supports x86, x86_64, ARM, ARM64, and MIPS32 architectures.
Channel‑based producer‑consumer communication.
Provides semaphores and coroutine locks.
Native coroutine support for socket and stream modules with seamless thread‑coroutine switching.
Example HTTP/file server implementations require only a few hundred lines of code.
Both stackful and stackless modes; stackless coroutines occupy only tens of bytes and switch faster.
Integrates with epoll, kqueue, poll, select, and IOCP.
Allows simultaneous waiting and scheduling of sockets, pipes, and processes within coroutines and pollers.
Database Library
Unified URL‑based connection interface with iterator‑style result enumeration.
Built‑in support for SQLite3 and MySQL; extensible to other relational databases.
XML Library
Provides DOM and SAX parsing modes; SAX uses an external iterator for higher performance.
Stream‑based parsing enables simultaneous download, decompression, transcoding, and parsing with low memory usage.
Includes an XML writer for generating XML documents.
Memory Library
Linux‑kernel‑inspired memory‑pool architecture optimized for various allocation patterns.
Debug mode detects leaks, out‑of‑bounds accesses, overlapping allocations, and provides usage statistics.
Optimized for large, small, and string allocations, achieving O(1) performance in ~96 % of cases.
Container Library
Hash tables, linked lists, arrays, queues, stacks, min/max heaps, etc.
Custom element types and full iterator support.
Most containers support stream‑based serialization and deserialization.
Algorithm Library
Sorting: bubble, heap, quick, insertion.
Searching: linear, binary.
Traversal, deletion, and statistical algorithms.
Iterator‑based interfaces similar to STL, implemented in C for lightweight usage.
Network Library
HTTP client implementation.
Cookie handling.
DNS resolution with caching.
SSL support via OpenSSL, PolarSSL, and mbedTLS.
IPv4 and IPv6 compatibility.
Asynchronous mode via coroutine integration.
Math Library
Fixed‑point arithmetic with various precisions.
Random number generator.
libc Library
Lightweight, cross‑platform libc implementation with architecture‑specific optimizations.
Comprehensive string and wide‑string operations, including case‑insensitive functions.
Optimized memset_u16 and memset_u32 for graphics rendering.
libm Library
Lightweight implementations of common math functions.
Integer versions of sqrt, log2, etc., avoiding floating‑point operations for embedded use.
Object Library
CoreFoundation‑style object system supporting objects, dictionaries, arrays, strings, numbers, dates, data, and custom serialization.
Serializes to XML, JSON, binary, and Apple plist formats; includes a custom binary format with simple encryption and ~30 % size reduction versus bplist.
Platform Library
File, directory, socket, thread, and time interfaces.
Atomic and atomic64 operations.
High‑precision and low‑precision timers.
High‑performance thread pool.
Event, mutex, semaphore, and spinlock primitives.
Stack trace retrieval for debugging.
Cross‑platform dynamic library loading (when supported).
IO poller abstracting epoll, poll, select, and kqueue.
Cross‑platform context‑switch interface optimized for coroutine usage.
Compression Library
Supports zlib, zlibraw, and gzip compression/decompression (requires third‑party zlib).
Character‑Set Library
Conversion between UTF‑8, UTF‑16, GBK, GB2312, UC2, UC4 with endian support.
Utility Library
Base64/32 encoding and decoding.
Hash algorithms: CRC32, Adler32, MD5, SHA1.
Logging, assertions, and other debugging helpers.
URL encoding/decoding.
Bit‑operation utilities for parsing various data widths and endianness, with optimized static_stream and stream wrappers.
Byte‑swap functions (swap16, swap32, swap64) with platform‑specific optimizations.
Fast bit‑counting functions (population count, leading/trailing zeros/ones).
Thread‑safe singleton module.
Option module for command‑line argument parsing.
Regex Library
Matching and replacement with global, multiline, and case‑insensitive modes.
Backed by PCRE, PCRE2, and POSIX regex engines.
Projects Using TBOX
gbox – https://github.com/tboox/gbox
vm86 – https://github.com/tboox/vm86
xmake – http://www.xmake.io/cn
itrace – https://github.com/tboox/itrace
Compilation
First install xmake: https://github.com/xmake-io/xmake
# Default: compile for the host platform
$ cd ./tbox
$ xmake
# Compile for mingw
$ cd ./tbox
$ xmake f -p mingw --sdk=/home/mingwsdk
$ xmake
# Compile for iphoneos
$ cd ./tbox
$ xmake f -p iphoneos
$ xmake
# Compile for android
$ cd ./tbox
$ xmake f -p android --ndk=xxxxx
$ xmake
# Cross‑compile for linux with custom SDK
$ cd ./tbox
$ xmake f -p linux --sdk=/home/sdk #--bin=/home/sdk/bin
$ xmakeExample
#include "tbox/tbox.h"
int main(int argc, char** argv)
{
// init tbox
if (!tb_init(tb_null, tb_null)) return 0;
// trace
tb_trace_i("hello tbox");
// init vector
tb_vector_ref_t vector = tb_vector_init(0, tb_element_str(tb_true));
if (vector)
{
// insert items
tb_vector_insert_tail(vector, "hello");
tb_vector_insert_tail(vector, "tbox");
// dump all items
tb_for_all(tb_char_t const*, cstr, vector)
{
tb_trace_i("%s", cstr);
}
// exit vector
tb_vector_exit(vector);
}
// init stream
tb_stream_ref_t stream = tb_stream_init_from_url("http://www.xxx.com/file.txt");
if (stream)
{
// open stream
if (tb_stream_open(stream))
{
// read lines
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);
}
}
// exit stream
tb_stream_exit(stream);
}
// wait for input before exit
tb_getchar();
tb_exit();
return 0;
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
