Skip to content

cyn1x/libcspd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

159 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libcspd

License Language Platform

A cross-platform C library providing core data structures and utilities for efficient C program development. The purpose of these utilities is to provide a simple interface for leveraging common data structures when programming in C.

/**
 * Simple program demonstrating one of many utilities available in libcspd.
 */

#include <cspd_assert.h>
#include <cspd_print.h>
#include <cspd_types.h>
#include <cspd_vector.h>

// Data structure representing a point in two-dimensional space
typedef struct point_t
{
    f32 x;
    f32 y;
} point;

// Declare vector with NAME prefix "point", and TYPE point
cspd_vector_declare(point, point);

/**
 * Program entry point
 */
int main(int argc, char *argv[])
{
    // Create a vector to store a contiguous array of elements
    point_vector point_vec;
    point_vector_init(&point_vec);

    // Create three point structures to represent a triangle
    point point_a = { 0.5,  1.0};
    point point_b = { 0.5, -0.5};
    point point_c = {-0.5,  0.5};

    // Push the three new points into the vector
    point_vector_push(&point_vec, &point_a);
    point_vector_push(&point_vec, &point_b);
    point_vector_push(&point_vec, &point_c);

    cspd_assert(point_vec.size == 3);

    printf("a triangle has %zu points\n", point_vec.size);
    // Output: a triangle has 3 points

    point_vector_clear(&point_vec);

    return 0;
}

ℹ️ There is also support for integrating custom memory allocators via the cspd_mem.h interface. The default behaviour uses the stdlib.h memory functions.

Installation

This program can be compiled on both Windows and Linux by cloning the repository and running the respective build scripts after installing the required dependencies.

ℹ️ Currently only supports x86 and x64 architectures.

Windows

MSVC is used to build the project on Windows, and Doxygen is used to generate documentation.

Dependencies

  • Microsoft Visual Studio Community (>= 2022) or Build Tools (>= 2022)
  • Doxygen
Installing Visual Studio

Microsoft Visual Studio Build Tools can be used to compile the project library DLL as an alternative to a full IDE installation.

Use the Visual Studio Installer (GUI) to install the Workloads and Individual components.

  • Workloads
    • Desktop Development for C++
  • Individual components
    • C++ Clang Compiler for Windows (>=17.0.3)

Note: The Individual components are optional for the LSP configuration if using a text editor for development. If a full IDE is desired, install Visual Studio Community.

winget install --id=Microsoft.VisualStudio.2026.Community -e 

Otherwise, install Visual Studio Build Tools instead.

winget install --id=Microsoft.VisualStudio.2026.BuildTools -e 

Linux

The LLVM Clang compiler toolchain is used to build the project on Linux. The minimum Clang version tested is 17.0.6, though any newer version should work.

Dependencies

sudo apt-get install clang
Installing a specific Clang version manually
curl -O https://github.com/llvm/llvm-project/releases/download/llvmorg-17.0.6/clang+llvm-17.0.6-x86_64-linux-gnu-ubuntu-22.04.tar.xz
tar -xf ./clang+llvm-17.0.6-x86_64-linux-gnu-ubuntu-22.04.tar.xz
cd clang+llvm-17.0.6-x86_64-linux-gnu-ubuntu-22.04/bin/
./clang
sudo cp -R clang+llvm-17.0.6-x86_64-linux-gnu-ubuntu-22.04/* /usr/

Building

Linux

git clone https://github.com/cyn1x/libcspd.git
cd libcspd/
make

Windows

git clone https://github.com/cyn1x/libcspd.git
cd libcspd
tools\build.bat

The build script attempts to locate the Visual Studio install directory automatically. To override this, create a file named env.ini in the project root and set the path to vcvarsall.bat:

MsvcDir="C:\Apps\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat"

Usage

For use in other projects, both the Windows and Linux build scripts show an example of how to link the library to another program. The test program in the bin directory does exactly this.

Development

The project is currently being developed using Neovim. There is no strict development environment enforced.

Windows

Run the devenv.bat batch script to configure the MSVC environment. The platform will be automatically detected (x86 for 32-bit, x64 for 64-bit).

If using Visual Studio Community, open libcspd.sln and press F11 to start debugging.

Linux

Ensure Clang is available on the system PATH.

Tests

Windows

cd libcspd
bin\win32\Debug_x64\libcspd_x64.exe

Linux

cd libcspd/
LD_LIBRARY_PATH="/absolute/path/to/libcspd/lib/linux/Debug_x64;$LD_LIBRARY_PATH" ./bin/linux/Debug_x64/libcspd

Memory leaks can be checked with Valgrind:

LD_LIBRARY_PATH="/absolute/path/to/libcspd/lib/linux/Debug_x64;$LD_LIBRARY_PATH" valgrind --leak-check=yes ./bin/linux/Debug_x64/libcspd

Documentation

Doxygen is used to generate documentation. The resulting website will be generated in the /doc directory.

Windows

tools\doc.bat

Linux

tools\doc.sh

Roadmap

Legend:

✅ done  |  🔄 in progress  |  ⏳ planned

Current features:

  • ✅ Vector
  • ✅ Linked List
  • ✅ Stack
  • ✅ Queue
  • ✅ Binary Tree

Future Plans:

  • 🔄 Binary Search Tree
  • 🔄 Hash map
  • 🔄 Heap
  • ⏳ B-tree
  • ⏳ R-tree
  • ⏳ AVL Tree

Housekeeping:

  • Benchmarks
  • Basic demo application
  • Host Doxygen documentation online
  • C++ wrappers for incompatible C functions
  • Memory Allocator Plugin

Background

This project started as a way to learn data structures and algorithms from the ground up, and grew into a set of tools I now use in my own pet projects after I decided to discard C++ for personal use entirely. I felt I was spending more time learning a language with an astronomical number of generational changes rather than doing the thing. I felt as though my confusion with abstractions in C++ was a sign of incompetence. However, it turned out that I was more interested in what goes on "under the hood", so I decided to write my own standard library. I have nothing against C++, or any other tool for that matter. If C++ was required in my working life, I would have no problem spending the time to be proficient at it through textbooks, conferences, and other mediums. Given my professional life does not require C++ — I could not care less for any of that, and I would rather do things from scratch and be more connected to the systems I write.

It's published here as a reference and source of inspiration for anyone building their own utility library or learning how common data structures are implemented in C.

Contributions, bug reports, and forks are welcome.

About

Cross-platform C library providing core data structures and utilities for efficient C program development.

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors