[GCC library compare] GCC static and share
hello.h
void hello();
hello.c
#include <stdio.h>
void hello(){
printf("hello world\n");
}
main.c
#include <stdio.h>
#include "hello.h"
int main(){
printf("call hello()");
hello();
}
##############################
Share library
gcc -fPIC -shared hello.c -o libhello.so
gcc main.c -L. -lhello -o main
Q:
./main
./main: error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory
A:
sudo cp libhello.so /usr/lib/
./main
call hello()hello world
##############################
Static library
gcc -c hello.c -o hello.o
ar -r libhello.a hello.o # put *.o to *.a. This command like tar.
gcc main.c -lhello -L. -static -o main
./main
call hello()hello world
Reference :
0 意見:
張貼留言