Code Monkey home page Code Monkey logo

headerlessc's Introduction

Headerless C programming with a single macro definition

Header files in C are painful. They duplicate file count, increase complexity, make refactoring painful. There are solutions to get rid of them. It's possible to use header generators ( https://www.hwaci.com/sw/mkhdr/ - makeheaders ), write you own headerless c dialect with a precompiler like me ( https://github.com/milgra/clc class-c ) or use "#ifdef FOO_IMPLEMENTATION" blocks inside header files to define everything in one file but they are unelegant and confusing and have a lot of other problems.

The ultimate solution seems to be using the __INCLUDE_LEVEL__ preprocessor macro. It's value is zero if we are in a source file that was added directly to the compiler as parameter and greater than zero if we are in a file that was included as a header file from an other file.

So just create a single file, write the header declarations at the top, write the implementation under that and guard the implementation with an #if __INCLUDE_LEVEL__ == 0 macro and you never have to use header files again unless you need a library interface. You can include all files written this way as header files and add these files as source files to the compiler, everything will work as before.

Example : mtvec.c

#ifndef mtvec_h
#define mtvec_h

#include <stdio.h>
#include <stdint.h>

typedef struct mtvec_t mtvec_t;

struct mtvec_t
{
    void** data;
    uint32_t length;
    uint32_t length_real;
};

mtvec_t* mtvec_alloc(void);
void mtvec_dealloc( void* vector );
void mtvec_reset( mtvec_t* vector );

#endif

#if __INCLUDE_LEVEL__ == 0

mtvec_t* mtvec_alloc( )
{
    mtvec_t* vector = mtmem_calloc( sizeof( mtvec_t ) , mtvec_dealloc );
    vector->data = mtmem_calloc( sizeof( void* ) * 10 , NULL );
    vector->length = 0;
    vector->length_real = 10;
    return vector;
}

void mtvec_dealloc( void* pointer )
{
    mtvec_t* vector = pointer;

    for ( uint32_t index = 0 ; index < vector->length ; index++ ) {
    mtmem_release( vector->data[index] );
    }
    mtmem_release( vector->data );
}

void mtvec_reset( mtvec_t* vector )
{
    for ( uint32_t index = 0 ; index < vector->length ; index++ ) mtmem_release( vector->data[index] );

    vector->length = 0;
}

#endif

I created/maintain seven applications so far in headerless C and haven't faced any drawbacks yet, feel free to try them :

headerlessc's People

Contributors

milgra avatar rodrigohahn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

headerlessc's Issues

Cool find

But I think its a GNU extension. It'd be amazing if it were ISO standard.

Module generator?

I found about this __INCLUDE_LEVEL__ some time ago, but I had something better in mind. Unfortunately this requires some parser that could at least understand some basic syntax tricks and can at least identify C (and maybe also C++) entities, where they begin and end and how to transform the "definition" into "declaration". This would be then the original source version from which this presented in the front page would be only generated:

module mtvec;

// Only import: contents of these header files will not be provided to those
// who include THIS one.
import <stdio.h>;
import <stdint.h>;

export typedef struct mtvec_t mtvec_t;

struct mtvec_t
{
    void** data;
    uint32_t length;
    uint32_t length_real;
};

export mtvec_t* mtvec_alloc( )
{
    mtvec_t* vector = mtmem_calloc( sizeof( mtvec_t ) , mtvec_dealloc );
    vector->data = mtmem_calloc( sizeof( void* ) * 10 , NULL );
    vector->length = 0;
    vector->length_real = 10;
    return vector;
}

export void mtvec_dealloc( void* pointer )
{
    mtvec_t* vector = pointer;

    for ( uint32_t index = 0 ; index < vector->length ; index++ ) {
    mtmem_release( vector->data[index] );
    }
    mtmem_release( vector->data );
}

export void mtvec_reset( mtvec_t* vector )
{
    for ( uint32_t index = 0 ; index < vector->length ; index++ ) mtmem_release( vector->data[index] );

    vector->length = 0;
}

All type definitions (typedefs and structured) with export will be "swallowed whole" into the "header part". Functions will get only the header extracted and put into the generated header part, unless they have inline modifier, in which case they will be also put into the header part as whole.

Structures that are not marked with export will get only the incomplete type definition in the header, if it is used anywhere in the function signatures, otherwise it will be not present in the header part at all.

The generated single source file for this one would have to have an appropriate name with specific extension so that if you use import mtvec; statement in another file it knows what to look for (let's say, the generated file will have a name mtvec.chi, so this instruction will be equivalent to import "mtvec.chi";. Simultanously, import <stdio.h>; would simply reach out to the exact header file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.