system-programming:linking-against-library
تفاوتها
تفاوت دو نسخهٔ متفاوت از صفحه را مشاهده میکنید.
| system-programming:linking-against-library [2024/04/19 18:03] – ایجاد شد pejman | system-programming:linking-against-library [2024/06/18 01:23] (فعلی) – حذف شد pejman | ||
|---|---|---|---|
| خط 1: | خط 1: | ||
| - | ====== استفاده از library ها ====== | ||
| - | |||
| - | ---- | ||
| - | ===== استفاده از shared library ===== | ||
| - | |||
| - | <code C> | ||
| - | // man 3 pow | ||
| - | /* pow - power function | ||
| - | * Math library (libm, -lm) | ||
| - | * / | ||
| - | */ | ||
| - | #include < | ||
| - | double pow(double x, double y); | ||
| - | </ | ||
| - | |||
| - | <code C> | ||
| - | /* pow.c */ | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | int main(void) | ||
| - | { | ||
| - | double base, exponent; | ||
| - | |||
| - | base = 2; | ||
| - | exponent = 10; | ||
| - | |||
| - | printf(" | ||
| - | base, exponent, pow(base, exponent)); | ||
| - | return 0; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | $ gcc pow.c -lm -o pow | ||
| - | $ ./pow | ||
| - | 2.000000 to the power 10.000000 : 1024.000000 | ||
| - | |||
| - | <code C> | ||
| - | /* interest.c */ | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | int main(void) | ||
| - | { | ||
| - | int years = 15; /* The number of years you will | ||
| - | * keep the money in the bank | ||
| - | * account */ | ||
| - | int savings = 99000; /* The inital amount */ | ||
| - | float interest = 1.5; /* The interest in % */ | ||
| - | |||
| - | printf(" | ||
| - | "is %.2f\n", | ||
| - | savings * pow(1+(interest/ | ||
| - | return 0; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | $ gcc interest.c -lm -o interest | ||
| - | $ ./interest | ||
| - | The total savings after 15 years is 123772.95 | ||
| - | |||
| - | ---- | ||
| - | |||
| - | |||
| - | ===== ساخت library شخصی ===== | ||
| - | |||
| - | <code C> | ||
| - | // prime.h | ||
| - | int isprime(long int number); | ||
| - | </ | ||
| - | |||
| - | <code C> | ||
| - | // prime.c | ||
| - | int isprime(long int number) | ||
| - | { | ||
| - | long int j; | ||
| - | int prime = 1; | ||
| - | |||
| - | for(j=2; j< | ||
| - | if(number%j == 0) | ||
| - | prime = 0; | ||
| - | |||
| - | if(prime == 1) | ||
| - | return 1; | ||
| - | else | ||
| - | return 0; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | ساخت object از نوع PIC : Position Independent Code | ||
| - | * [[https:// | ||
| - | |||
| - | |||
| - | $ # -Wall -Wextra -pedantic are warning options: | ||
| - | $ gcc -Wall -Wextra -pedantic -fPIC -c prime.c | ||
| - | $ ls -l prime.o | ||
| - | -rw-r--r-- 1 pejman pejman 1320 Oct 29 00:06 prime.o | ||
| - | |||
| - | ساخت shared Library | ||
| - | * [[https:// | ||
| - | |||
| - | $ # -Wl,... send option to linker | ||
| - | $ # .so : shared object | ||
| - | $ gcc -shared -Wl, | ||
| - | $ ls -l libprime.so | ||
| - | -rwxr-xr-x 1 pejman pejman 15528 Oct 29 00:12 libprime.so | ||
| - | |||
| - | |||
| - | استفاده از library شخصی در Home Directory | ||
| - | |||
| - | |||
| - | <code C> | ||
| - | /* complete version is-it-a-prime.c */ | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include " | ||
| - | |||
| - | int main(int argc, char *argv[]) | ||
| - | { | ||
| - | long int num; | ||
| - | |||
| - | /* Only one argument is accepted */ | ||
| - | if (argc != 2) | ||
| - | { | ||
| - | fprintf(stderr, | ||
| - | | ||
| - | return 1; | ||
| - | } | ||
| - | |||
| - | /* Only numbers 0-9 are accepted */ | ||
| - | if ( strspn(argv[1], | ||
| - | strlen(argv[1]) ) | ||
| - | { | ||
| - | fprintf(stderr, | ||
| - | " | ||
| - | return 1; | ||
| - | } | ||
| - | |||
| - | num = atol(argv[1]); | ||
| - | if (isprime(num)) /* Check if num is a prime */ | ||
| - | { | ||
| - | printf(" | ||
| - | } | ||
| - | else | ||
| - | { | ||
| - | printf(" | ||
| - | } | ||
| - | |||
| - | | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | کامپایل و لینک با library شخصی در Home Directory | ||
| - | |||
| - | $ gcc -L${PWD} is-it-a-prime.c -o is-it-a-prime -lprime | ||
| - | |||
| - | |||
| - | فایل is-it-a-prime با library مورد نظر در home directory کامپایل شده ولی ضمن اجرا library در دسترس نیست و error خواهد داد | ||
| - | |||
| - | $ ./ | ||
| - | ./ | ||
| - | |||
| - | می توان library هایی که در دسترس نیستند را به این شکل دید (not found) | ||
| - | |||
| - | $ ldd is-it-a-prime | ||
| - | | ||
| - | | ||
| - | | ||
| - | / | ||
| - | |||
| - | مگر آنکه Home directory به LD_LIBRARY_PATH اضافه شده باشد | ||
| - | |||
| - | $ export LD_LIBRARY_PATH=${PWD}: | ||
| - | $ ./ | ||
| - | Usage: ./ | ||
| - | $ ./ | ||
| - | 11 is a prime | ||
| - | $ ./ | ||
| - | 15 is not a prime | ||
| - | |||
| - | بررسی مجدد لیست library ها و دردسترس بودن libprime.so | ||
| - | |||
| - | $ ldd is-it-a-prime | ||
| - | | ||
| - | | ||
| - | | ||
| - | / | ||
| - | |||
| - | |||
| - | ---- | ||
| - | ===== ساخت library شخصی (خلاصه شده) ===== | ||
| - | |||
| - | <code C> | ||
| - | /* prime.h */ | ||
| - | int isprime(long int number); | ||
| - | </ | ||
| - | |||
| - | <code C> | ||
| - | /* prime.c */ | ||
| - | int isprime(long n) | ||
| - | { | ||
| - | for(long i=2; i < n/2; i++) | ||
| - | if(n % i == 0) return 0; | ||
| - | return 1; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <code C> | ||
| - | /* is-it-a-prime.c */ | ||
| - | #include < | ||
| - | #include " | ||
| - | |||
| - | int main(void) | ||
| - | { | ||
| - | long int num = 1000024073; | ||
| - | |||
| - | if (isprime(num)) | ||
| - | printf(" | ||
| - | else | ||
| - | printf(" | ||
| - | |||
| - | return 0; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | $ gcc -c prime.c -fPIC | ||
| - | $ gcc -shared -Wl, | ||
| - | $ gcc is-it-a-prime.c -o is-it-a-prime -lprime -L${PWD} | ||
| - | |||
| - | $ ldd ./ | ||
| - | | ||
| - | | ||
| - | | ||
| - | / | ||
| - | |||
| - | $ export LD_LIBRARY_PATH=${PWD}: | ||
| - | $ ldd ./ | ||
| - | | ||
| - | | ||
| - | | ||
| - | / | ||
| - | $ ./ | ||
| - | 1000024073 is a prime | ||
| - | |||
| - | < | ||
| - | $ nm -D ./ | ||
| - | w __cxa_finalize | ||
| - | w __gmon_start__ | ||
| - | 00000000000010f9 T isprime | ||
| - | w _ITM_deregisterTMCloneTable | ||
| - | w _ITM_registerTMCloneTable | ||
| - | </ | ||
| - | |||
| - | < | ||
| - | $ objdump -T --demangle libprime.so | ||
| - | |||
| - | libprime.so: | ||
| - | |||
| - | DYNAMIC SYMBOL TABLE: | ||
| - | 0000000000000000 | ||
| - | 0000000000000000 | ||
| - | 0000000000000000 | ||
| - | 0000000000000000 | ||
| - | 00000000000010f9 g DF .text 000000000000004e isprime | ||
| - | |||
| - | </ | ||
| - | |||
| - | man libc | ||
| - | man ldd | ||
system-programming/linking-against-library.1713537238.txt.gz · آخرین ویرایش: 2024/04/19 18:03 توسط pejman
