My Library is a C library template that provides a solid foundation for building C libraries with modern tooling and best practices.
- Building the Project
- Build Options
- Using as a CMake Subdirectory
- Count Malloc Calls
- Creating a New Project from This Template
- CMake 3.16 or higher
- C99 compatible compiler (GCC or Clang)
- Unix-like operating system (Linux, macOS, BSD)
- Git (for cloning)
# Clone the repository
git clone https://github.com/MyGroup/mylib.git
cd mylib
# Create build directory
mkdir build
cd build
# Configure and build
cmake ..
make# Build with all features enabled
cmake -DMYLIB_BUILD_SHARED=ON -DMYLIB_BUILD_EXAMPLE=ON -DMYLIB_BUILD_TESTS=ON ..
# Build in Release mode
cmake -DCMAKE_BUILD_TYPE=Release ..The project supports several CMake options to customize the build:
| Option | Default | Description |
|---|---|---|
CMAKE_BUILD_TYPE |
Debug |
Build type: Debug, Release, RelWithDebInfo, MinSizeRel |
MYLIB_BUILD_SHARED |
OFF |
Build library as a shared library instead of static |
MYLIB_BUILD_EXAMPLE |
ON (standalone) |
Build example program demonstrating library usage |
MYLIB_BUILD_TESTS |
ON (standalone) |
Build test programs |
- Debug: Includes debug symbols, no optimization, enables
__MYLIB_DEBUG__macro - Release: Optimized for performance, no debug symbols
- RelWithDebInfo: Optimized with debug symbols
- MinSizeRel: Optimized for minimal size
- GCC/Clang: Uses
-Wall -Wextra -Wpedanticwith additional warnings - Position-independent code enabled, C99 standard enforced
To use this library in your own CMake project:
# Add the library as a subdirectory
add_subdirectory(mylib)
# Link the library to your target
target_link_libraries(your_target PRIVATE mylib)The library will be built as a static library by default. To use as a shared library:
set(MYLIB_BUILD_SHARED ON)
add_subdirectory(mylib)
target_link_libraries(your_target PRIVATE mylib)In order to run this script install uv python package this manager
and run this command:
./analyze_malloc.pyThis script will show graph of malloc usage graph per commit.
This repository serves as a template for creating new C library projects. Use the included setup script to create your own project:
curl -fsSL "https://raw.githubusercontent.com/burs1/templ.c/refs/heads/main/setup.sh" | bash -s mynewlib- Clones the template: Creates a copy of this repository
- Replaces project name: Changes all "mylib" references to your project name
- Replaces full project name: Changes "My Library" references to your full project name
- Handles case variations: Updates
mylib,MYLIB, andMyLibappropriately - Initializes git: Creates a fresh git repository for your project
- Provides next steps: Shows instructions for customizing your new project