diff --git a/examples/libc99/libc99.c b/examples/libc99/libc99.c new file mode 100644 index 0000000..6256153 --- /dev/null +++ b/examples/libc99/libc99.c @@ -0,0 +1,28 @@ +#include "libc99.h" +#include +#include + +Test_struct1* new_test_struct(int c_int, Test_enum c_enum, const char* c_str) +{ + Test_struct1* result = calloc(1, sizeof(Test_struct1)); + if (result) { + result->c_int = c_int; + result->c_enum = c_enum; + if (c_str) { + result->c_str = strdup(c_str); + if (result->c_str == NULL) { + free_test_struct(result); + return NULL; + } + } + } + return result; +} + +void free_test_struct(Test_struct1* c_struct) +{ + if (c_struct) { + free(c_struct->c_str); + free(c_struct); + } +} diff --git a/examples/libc99/libc99.h b/examples/libc99/libc99.h new file mode 100644 index 0000000..e641aca --- /dev/null +++ b/examples/libc99/libc99.h @@ -0,0 +1,37 @@ +// We are testing wrappers for the c-datatypes: +// * integral e.g (int) here +// * enum +// * c-string +// * struct +// Those are all considered POD-types +#ifdef __cplusplus +extern "C" { +#endif + + typedef enum + { + ONE, + TWO, + THREE + } Test_enum; + + typedef struct { + int c_int; + Test_enum c_enum; + char* c_str; + } Test_struct1; + + typedef struct { + int c_int; + Test_enum c_enum; + char* c_str; + Test_struct1* c_struct1; + } Test_struct2; + + + Test_struct1* new_test_struct(int c_int, Test_enum c_enum, const char* c_str); + + void free_test_struct(Test_struct1* c_struct); +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/examples/libc99/makefile b/examples/libc99/makefile new file mode 100644 index 0000000..592ac43 --- /dev/null +++ b/examples/libc99/makefile @@ -0,0 +1,27 @@ +LIB_STATIC=libc99.a +LIB_DYN=libc99.so + +CFLAGS+=-std=c99 -g -UNDEBUG + +SRCS+=$(wildcard *.c) +OBJS_C+=$(SRCS:.c=.o) + +.PHONY: all lib_static lib_dyn clean + +all: lib_static lib_dyn + +lib_static: $(LIB_STATIC) + +lib_dyn: $(LIB_DYN) + +$(LIB_STATIC): $(OBJS_C) + $(AR) -rc $@ $^ + +$(LIB_DYN): $(OBJS_C) + $(CC) $(CFLAGS) -shared -o $@ $^ + +clean: + rm -f $(LIB_STATIC) + rm -f $(LIB_DYN) + rm -f $(OBJS_C) +