library
General
  • ldd, check share library needed
  • libABC.so.x.y.z
  • Dynamic Library

    gcc -c -fpic -I include src/util.c # generate util.o
    gcc -c -fpic -I include src/temp.c # generate temp.o
    gcc -shared -o lib/libhello.so util.o temp.o # generate dynamic library libhello.so
    gcc -o hello.e -I include src/main.c -L lib -lhello # compile main.c using dynamic library
    		

    # makefile
    VPATH = src:include
    SRC = src
    LIB = lib
    CFLAGS = -I include
    objects = main.c util.o temp.o libhello.so
    cc = gcc
    hello.e: $(objects)
    	$(cc) -o hello.e $(CFLAGS) $< -L $(LIB) -lhello
    libhello.so: util.o temp.o
    	$(cc) -shared -o $(LIB)/libhello.so $^
    temp.o: temp.c temp.h
    	$(cc) -c -fpic $(CFLAGS) $< -o $@
    util.o: util.c util.h
    	$(cc) -c -fpic $(CFLAGS) $< -o $@
    .PHONY: clean
    clean: # no dependencies, not implemented automatically
    	rm -f *.o *.e *~ $(LIB)/*.so
    		
  • Making library available at run time
  • Static Library
    gcc -c -I include src/util.c # generate util.o
    gcc -c -I include src/temp.c # generate temp.o
    ar rcs lib/libhello.a util.o temp.o # generate static library libhello.a
    gcc -o hello.e src/main.c -I include -L lib -lhello # compile main.c using static library
    		
    # makefile
    VPATH = src:include
    SRC = src
    LIB = lib
    CFLAGS = -I include
    objects = main.c util.o temp.o libhello.a
    cc = gcc
    hello.e: $(objects)
    	$(cc) -o hello.e $(CFLAGS) $< -L $(LIB) -lhello
    libhello.a: util.o temp.o
    	ar rcs $(LIB)/libhello.a $^
    temp.o: temp.c temp.h
    	$(cc) -c $(CFLAGS) $< -o $@
    util.o: util.c util.h
    	$(cc) -c $(CFLAGS) $< -o $@
    .PHONY: clean
    clean: # no dependencies, not implemented automatically
    	rm -f *.o *.e *~ $(LIB)/*.a
    		
  • Making library available at run time
  • Reference
  • https://akkadia.org/drepper/dsohowto.pdf
  • Shared libraries with GCC on Linux