Linux

In Linux a shared library (.so) file can be created from a source foo.c as follows

gcc -c -fpic foo.c
gcc -shared -o libfoo.so foo.o

or, directly

gcc -fpic -shared -o libfoo.so foo.c

Make sure that the environment variable LD_LIBRARY_PATH contians the path where the above generated .so file can be found.

MacOS

On MacOS use the following

gcc -fpic -dynamiclib -o libfoo.dylib foo.c

Make sure that the environment variable DYLD_LIBRARY_PATH contians the path where the above generated .dylib file can be found.