Creating a Shared Library - PowerPoint PPT Presentation

About This Presentation
Title:

Creating a Shared Library

Description:

Compile the files comb.c' and other.c' Build a shared library containing both. Compile the uselib.cpp' demo-program so it will be linked with your shared library ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 13
Provided by: ProfessorA
Learn more at: https://www.cs.usfca.edu
Category:

less

Transcript and Presenter's Notes

Title: Creating a Shared Library


1
Creating a Shared Library
  • How you can create and use your own object-code
    libraries within the Linux environment

2
Building an executable file
source text
linker
compiler
object code
executable file
Translates programming language statements
into cpus machine-language
instructions
Adjusts any memory references to fit the
Operating Sytems memory model
3
Example source text in assembly
  • .data
  • msg string Hello \n
  • .text
  • main movl 4, eax
  • movl 1, ebx
  • movl msg, ecx
  • movl 8, edx
  • int 0x80
  • ret
  • .globl main

4
Compiling the source text
  • gcc c hello.s compile
  • ld hello.o o hello link

5
Normally need a runtime library
source text
object file
compiler
linker
executable file
object code library
A previously compiled collection of standard
program functions
6
How to build/use an archive file
  • gcc c func1.c
  • gcc c func2.c
  • ar cr myarchive.a func1.o func.o
  • g myapp.cpp myarchive.a o myapp

7
Static versus Dynamic Linking
  • gcc c hello.s
  • ld static hello.o o hello
  • or
  • ld shared hello.o o hello

8
Smaller is more efficient
objject file
object file
static linking
function library
function library
executable files
object file
object file
dynamic linking
pointer
pointer
shared function library
9
Building a Shared Library
  • Functions must be reentrant
  • This implies No global variables
  • Code must be position-independent
  • Cant jump or call to fixed addresses
  • Cant access data at fixed locations

10
Command-formats
  • For building the shared library
  • gcc c fPIC func.c
  • gcc shared fPIC func.o o libfunc.so
  • For linking application with shared library
  • g app.cpp -oapp L. lfunc Wl,-rpath,.

11
Command-line options
  • You need the -L. option to tell the linker
    where to search the for your libxxx.so files
  • You need the -l option to tell the linker which
    shared object files to link with first
  • You need the -Wl option to tell gcc/g
    compilers to pass options on to the linker
  • See the man pages for additional details

12
Class Exercise
  • Compile the files comb.c and other.c
  • Build a shared library containing both
  • Compile the uselib.cpp demo-program so it will
    be linked with your shared library
  • Run the uselib demo-program and turn in a
    printout that shows its output
Write a Comment
User Comments (0)
About PowerShow.com