https://github.com/alphatechnolog/libcommon
libcommon is a simple library which contains abstractions for some utilities such as dynamic arrays, optionals, defer statements, etc
https://github.com/alphatechnolog/libcommon
c
Last synced: 5 months ago
JSON representation
libcommon is a simple library which contains abstractions for some utilities such as dynamic arrays, optionals, defer statements, etc
- Host: GitHub
- URL: https://github.com/alphatechnolog/libcommon
- Owner: AlphaTechnolog
- Created: 2024-07-31T02:43:04.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-22T14:00:45.000Z (almost 2 years ago)
- Last Synced: 2025-07-19T17:45:17.485Z (12 months ago)
- Topics: c
- Language: C
- Homepage:
- Size: 44.9 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Libcommon
libcommon is a simple C library which provides abstractions for dynamic arrays, optionals, defer statements, etc.
## Linking against libcommon
First you will have to build the library, this example assumes you have a folder (git initialised) with something like this:
```
src
main.c
extern
```
so you will have to do something like this to build the project
```sh
cat << "EOF" > src/main.c
#include
#define LIBCOMMON_ENABLE_EXPERIMENTAL_DEFER
#include
typedef struct person_t {
const char *name;
const char *lastname;
} *Person;
Person new_person(const char *name, const char *lastname) {
Person self = Common_dsmalloc(Person);
self->name = name;
self->lastname = lastname;
return self;
}
int main() {
Optional opt_person = Common_optional_alloc_with((void*) new_person("John", "Doe"));
defer({ Common_optional_free(opt_person); });
if (Common_optional_is_some(opt_person)) {
const Person john = Common_optional_unpack(opt_person);
printf("Hello %s %s\n", john->name, john->lastname);
}
}
EOF
cd extern
git submodule add https://github.com/alphatechnolog/libcommon libcommon
cd libcommon
make
cd ../../
cc -c -o main.o src/main.c -Iextern/libcommon/include -O3 -Werror -Wall
cc -o application main.o -Lextern/libcommon/lib -lcommon
./application
```
So this was an example of using libcommon's optionals and the defer statement provided by libcommon... take in mind that enabling
libcommon's defer statement can be problematic if you're targeting another compiler different than gcc, this is because defer atm is only supported for gcc because of how the macro is done. Anyways, I am still working on porting it for other compilers :(
## Documentation
Pretty WIP rn but you can take a look at the [examples](./examples) in the source code