From 287c98d8ff034dc8439c09893e57828bb7dc203d Mon Sep 17 00:00:00 2001 From: Filip Date: Sun, 22 Dec 2024 23:49:01 +0100 Subject: [PATCH] Add howto.md --- howto.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 howto.md diff --git a/howto.md b/howto.md new file mode 100644 index 0000000..7b180e5 --- /dev/null +++ b/howto.md @@ -0,0 +1,39 @@ +1. Declare functions in a header file using ifndef macros to prevent multiple includes: + +```c +/* example.h file */ +#ifndef EXAMPLE_H +#define EXAMPLE_H + +void example(void); + +#endif +``` + +Do this for each file. In this example `another-example.c`, `third-example.c`. + +2. Create implementation / definition of that function: + +```c +/* example.c file */ +void example(void); +{ + /* do something */ +} +``` + +3. Compile to object file: `clang -c example.c another-example.c third-example.c`. You can also do `clang -c *.c`. + +To create archive (static library): `llvm-ar r outputname.a example.c another-example.c third-example.c` + +To create shared object (dynamic library): `ld.lld --shared -o outputname.so example.c another-example.c third-example.c` + +Use either with your main: + +Archive: `clang main.c outputname.a` + +--- + +Shared object: `clang main.c outputname.so` + +On Linux also set correct LD_LIBRARY_PATH with `export LD_LIBRARY_PATH=/path/to/outputname.so` \ No newline at end of file