Home

Awesome

C++ Standard Library and Qt, Python etc Implementation in C

This project aims to reimplement the C++ standard library functionality using the C programming language. It provides C developers with tools and libraries commonly available in C++, enabling better data structure management, algorithm implementation, and feature usage while staying within the C language's ecosystem.

A Personal Note from Me

I undertake this project out of a deep affection for the C programming language. It is my belief that C remains an essential tool for any computer engineer or programmer, providing the foundation necessary to build efficient and robust software. My love for C drives this endeavor, aiming to enrich the language with the familiar conveniences found in C++.

Project Structure

The project is organized into several subdirectories, each representing a different module of the standard library:


Note

Each module in the project comes with a .c source file, a .h header file, and a README.md file. These README files offer detailed explanations of each module's functionality, usage examples, and any other relevant information, ensuring that developers can easily understand and utilize the components of the library.

Compilation and Execution

CMake Support

This project now supports building with CMake, a powerful cross-platform build system that simplifies the process of compiling and linking code.

Building with CMake

To build the project using CMake, follow these steps:

  1. Install CMake:

    • Ensure that CMake is installed on your system. You can download it from the official website or use a package manager like apt, brew, or choco depending on your operating system.
  2. Create a Build Directory:

    • Navigate to the root directory of the project and create a build directory:

      mkdir build
      cd build
      
  3. Generate Build Files:

    • Run CMake to generate the build files for your system:

      cmake -G "Unix Makefiles" ..
      
  4. Compile the Project:

    • Once the build files are generated, compile the project using make (or another build tool depending on your system):

      make
      
  5. Run the Compiled Program:

    • After compilation, the executable and shared libraries will be located in the build directory. You can run the executable directly:

      ./main
      
    • On Linux, ensure to set the LD_LIBRARY_PATH if you're using shared libraries:

      export LD_LIBRARY_PATH=./build:$LD_LIBRARY_PATH
      ./build/main
      

Adding Modules with CMake

If you add new modules, you need to update the CMakeLists.txt file to include them. This file is used by CMake to configure the build process. Here's how to add a new module:

  1. Edit CMakeLists.txt:

    • Add the new module's source files to the add_library or add_executable commands in the CMakeLists.txt file.
  2. Re-run CMake:

    • After editing CMakeLists.txt, re-run CMake in the build directory:

      cmake -G "Unix Makefiles" ..
      
  3. Compile Again:

    • Use make or another build tool to compile the updated project.

This project utilizes a Python script (compile.py) for easy compilation of modules, making the build process straightforward and efficient.

Requirements

Using the compile.py Script

To compile the entire project, simply run the compile.py script with the b argument:

python compile.py b

This command compiles all source files and produces an executable in the ./build directory.

Running the Compiled Program

To compile and immediately run the compiled program, use the r argument:

python compile.py r

On Linux, make sure to set the LD_LIBRARY_PATH before running the program:

export LD_LIBRARY_PATH=./build:$LD_LIBRARY_PATH
./build/main

Compiling to Shared Libraries Only

To compile only the shared libraries (DLLs or .so files) for each module, use the l argument:

python compile.py l

This command compiles all source files into shared libraries in the ./build directory without producing an executable.

Adding New Modules

If you add new modules or directories containing .c files, simply include their paths in the source_directories list within the compile.py script. The script automatically finds and compiles all .c files in the specified directories.

Streamlined Build Process

The use of compile.py eliminates the need for traditional makefiles or manual compilation commands, providing a simple and unified build process. The script handles dependencies, includes, and linking, ensuring a hassle-free compilation experience.

Manual Compilation Using GCC

For developers who prefer manual compilation or need to integrate the project into other build systems, the source files can be compiled using the GCC command line. While the compile.py script is recommended for its convenience and automated handling of file dependencies, manual compilation offers flexibility for advanced use cases.

Requirements for Manual Compilation

Compiling with GCC

To manually compile a specific module or your entire project, you can use the GCC command with the -std=c17 flag to ensure compliance with the C17 standard. Here's an example command to compile a program with the vector module:

gcc -std=c17 -O3 -march=native -flto -funroll-loops -Wall -Wextra -pedantic -s -o your_program your_program.c vector.c

In this command:

Customizing the Compilation

You can modify the GCC command to suit your specific requirements, such as including additional modules, linking libraries, or adjusting optimization levels. This approach offers full

control over the compilation process, allowing you to tailor it to your project's needs.


Individual READMEs for Libraries

Each library module comes with its own README.md file, providing detailed instructions, sample code, function descriptions, and other relevant usage information.

Contribution

Contributions are welcome. Whether it's extending existing libraries, improving performance, or fixing bugs, your help is appreciated. Fork the repository, make your changes, and submit a pull request.

License

This project is open-source and available under ISC License.