static/shared_lib example added

This commit is contained in:
Kolan Sh 2012-05-21 18:58:30 +04:00
parent ea9e3ce17c
commit 565da568de
4 changed files with 41 additions and 0 deletions

20
c/shared_lib/build.sh Executable file
View File

@ -0,0 +1,20 @@
#!/bin/sh
CC=cc
LIBNAME=mean
LIBREV=1
LIBVER=1.0.0
TEST=main
rm *.out *.o *.a *.0 *.1 ${TEST}_* *.so &>/dev/null
# --- dynamic ---
$CC -c -fPIC $LIBNAME.c -o $LIBNAME.o
$CC -shared -Wl,-soname,lib${LIBNAME}.so.${LIBREV} -o lib${LIBNAME}.so.$LIBVER $LIBNAME.o
ldconfig -n .
ln -s lib${LIBNAME}.so.${LIBREV} lib${LIBNAME}.so
$CC $TEST.c -L. -l${LIBNAME} -o ${TEST}_dynamic
# --- static ---
ar rcs lib${LIBNAME}.a $LIBNAME.o
$CC $TEST.c -static -L. -l${LIBNAME} -o ${TEST}_static
# --- run dynamic ---
LD_LIBRARY_PATH=. ./main_dynamic

15
c/shared_lib/main.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include "mean.h"
int main(int argc, char* argv[]) {
double v1, v2, m;
v1 = 5.2;
v2 = 7.9;
m = mean(v1, v2);
printf("The mean of %3.2f and %3.2f is %3.2f\n", v1, v2, m);
return 0;
}

5
c/shared_lib/mean.c Normal file
View File

@ -0,0 +1,5 @@
//#include <stdio.h>
double mean(double a, double b) {
return (a+b) / 2;
}

1
c/shared_lib/mean.h Normal file
View File

@ -0,0 +1 @@
double mean(double, double);