Linking/Deferred linking for binary packages

From Sidvind
Jump to: navigation, search

libfoo.so may be distributed as a binary package and app.c foo.h would be distributed as source-code and built on the clients. libfoo.so has unresolved references which is resolved when building app on the client.

This helps with two problems:

  1. Library versions, if a library has changed its version number but the change is ABI compatible with libfoo.so a simple rebuild of app will resolve this (just as rebuilding source-based packages)
  2. Library path issues, some distributions place libraries in non-standard locations and maybe a dependency is installed by the user in their home-directory. This can also be fixed using symlinks or by fiddling with LD_LIBRARY_PATH but deferred linking is better in long term.

Code: app.c (view, download)

  1. #include "foo.h"
  2.  
  3. int main(int argc, const char* argv[]){
  4.   return real_main(argc, argv);
  5. }

Code: foo.h (view, download)

  1. #ifndef __FOO_H
  2. #define __FOO_H
  3.  
  4. int real_main(int argc, const char* argv[]);
  5.  
  6. #endif /* __FOO_H */

Code: foo.c (view, download)

  1. #include "foo.h"
  2. #include <stdio.h>
  3. #include <jpeglib.h>
  4.  
  5. int real_main(int argc, const char* argv[]){
  6.   printf("in real_main\n");
  7.  
  8.   struct jpeg_compress_struct cinfo;
  9.   struct jpeg_error_mgr jerr;
  10.  
  11.   cinfo.err = jpeg_std_error(&jerr);
  12.   jpeg_create_compress(&cinfo);
  13.  
  14.   return 0;
  15. }

Code: Makefile (view, download)

  1. all: app
  2.  
  3. libfoo.so: CFLAGS+=-fPIC
  4.  
  5. libfoo.so: foo.o
  6.         $(CC) -shared $(LDFLAGS) foo.o -o libfoo.so
  7.  
  8. app: app.o libfoo.so
  9.         $(CC) $(LDFLAGS) app.o -Wl,-rpath,. -L. -lfoo -ljpeg -o app
  10.  
  11. %.o : %.c
  12.         $(CC) -Wall $(CFLAGS) -c $< -o $@
  13.  
  14. clean:
  15.         rm -rf *.so *.o app