From 53af2aae688e973fc7fdd57068daf5ae96ba7ea0 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 13 May 2023 01:09:02 +0200 Subject: [PATCH] Add example of separating inline & function code --- example/Makefile | 12 +++++++++++- example/exm-header.h | 14 ++++++++++++++ example/exm-lib.c | 4 ++++ example/exm-main.c | 14 ++++++++++++++ example/exn-header.h | 19 +++++++++++++++++++ example/exn-lib.c | 4 ++++ example/exn-main.c | 19 +++++++++++++++++++ 7 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 example/exm-header.h create mode 100644 example/exm-lib.c create mode 100644 example/exm-main.c create mode 100644 example/exn-header.h create mode 100644 example/exn-lib.c create mode 100644 example/exn-main.c diff --git a/example/Makefile b/example/Makefile index 04d99bd6..80e0b58c 100644 --- a/example/Makefile +++ b/example/Makefile @@ -21,7 +21,7 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. CC=cc -std=c99 -CFLAGS=-Wall -W -pedantic -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wswitch-default -Wswitch-enum -Wcast-align -Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 -Wstrict-prototypes -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wunreachable-code -Wlogical-op -Wstrict-aliasing=2 -Wredundant-decls -Wold-style-definition -Wno-unused-function -g +CFLAGS=-Wall -W -pedantic -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wswitch-default -Wswitch-enum -Wcast-align -Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 -Wstrict-prototypes -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wunreachable-code -Wlogical-op -Wstrict-aliasing=2 -Wredundant-decls -Wold-style-definition -Wno-unused-function -g GMP_DIR= MPFR_DIR=$(GMP_DIR) CPPFLAGS=-I.. @@ -33,9 +33,19 @@ RM=rm -f .c.exe: $(CC) $(CFLAGS) $(CPPFLAGS) $< -o $@ `cat config` $(LDFLAGS) +.c.o: + $(CC) $(CFLAGS) $(CPPFLAGS) -O2 -ffunction-sections -fdata-sections -DNDEBUG -g $< -c -o $@ `cat config` $(LDFLAGS) + all: config $(MAKE) `ls ex-*.c|sed -e 's/\.c/\.exe/g'` if test "$(CC)" = "cc -std=c99" ; then $(MAKE) `ls ex11-*.c|sed -e 's/\.c/\.exe/g'` CC="cc -std=c11" ; fi + $(MAKE) exm.exe + +exm.exe: exm-lib.o exm-main.o + $(CC) -Wl,--gc-sections -g $^ -o $@ + +exn.exe: exn-lib.o exn-main.o + $(CC) -Wl,--gc-sections -g $^ -o $@ config: printf "#include \nint main(void){mpz_t z; mpz_init(z); return 0;}\n" > config-test.c diff --git a/example/exm-header.h b/example/exm-header.h new file mode 100644 index 00000000..6779271b --- /dev/null +++ b/example/exm-header.h @@ -0,0 +1,14 @@ +#ifndef MY_HEADER +#define MY_HEADER + +// Request M*LIB to not inline functions if not the library source +#ifndef M_USE_EXTERN_DEF +#define M_USE_EXTERN_DECL +#endif +#include "m-string.h" +#include "m-array.h" + +ARRAY_DEF(array_str, string_t) + +#endif + diff --git a/example/exm-lib.c b/example/exm-lib.c new file mode 100644 index 00000000..aba0530c --- /dev/null +++ b/example/exm-lib.c @@ -0,0 +1,4 @@ +// Request M*LIB to define functions +// This include both m-string functions and array_str templated functions +#define M_USE_EXTERN_DEF +#include "exm-header.h" diff --git a/example/exm-main.c b/example/exm-main.c new file mode 100644 index 00000000..c2d09b42 --- /dev/null +++ b/example/exm-main.c @@ -0,0 +1,14 @@ +#include "exm-header.h" + +int main(void) +{ + array_str_t a; + array_str_init(a); + array_str_emplace_back(a, "Hello"); + array_str_emplace_back(a, "World"); + printf("a="); + array_str_out_str(stdout, a); + printf("\n"); + array_str_clear(a); + return 0; +} diff --git a/example/exn-header.h b/example/exn-header.h new file mode 100644 index 00000000..5bf563ef --- /dev/null +++ b/example/exn-header.h @@ -0,0 +1,19 @@ +#ifndef MY_HEADER +#define MY_HEADER + +// Request M*LIB to not always inline functions +#define M_USE_EXTERN_FINE_GRAINED 1 +#include "m-string.h" +#include "m-array.h" + +// The following functions will not be inlined: +#undef M_USE_EXTERN_FINE_SELECT +#ifdef EXN_LIB +#define M_USE_EXTERN_FINE_SELECT 3 +#else +#define M_USE_EXTERN_FINE_SELECT 2 +#endif + +ARRAY_DEF(array_str, string_t) + +#endif diff --git a/example/exn-lib.c b/example/exn-lib.c new file mode 100644 index 00000000..7ea7f1da --- /dev/null +++ b/example/exn-lib.c @@ -0,0 +1,4 @@ +// Request M*LIB to define functions for templated array functions. +#define EXN_LIB 3 +#include "exn-header.h" + diff --git a/example/exn-main.c b/example/exn-main.c new file mode 100644 index 00000000..e6c979c4 --- /dev/null +++ b/example/exn-main.c @@ -0,0 +1,19 @@ +#include "exn-header.h" + +int main(void) +{ + string_t s; + string_init_set_str(s, "a="); + string_fputs(stdout, s); + string_clear(s); + array_str_t a; + array_str_init(a); + array_str_emplace_back(a, "Hello"); + array_str_emplace_back(a, "World"); + //printf("a="); + array_str_out_str(stdout, a); + printf("\n"); + array_str_clear(a); + //main2(); + return 0; +}