Cpp programming on Linux using library static and dynamic
Utilfile.h
#include
void util();
Utilfile.c
#include "utilfile.h"
void util(){
printf("Util function");
}
main.c
#include "utilfile.h"
int main(){
util();
return 0;
}
Makefile static
main: main.o libstatic.a
g++ -static -I. -o main main.o -L. -lstatic
main.o: main.c
g++ -c -I. main.c
libstatic.a: utilfile.o
ar cr libstatic.a utilfile.o
utilfile.o: utilfile.c utilfile.h
g++ -c -I. utilfile.c
clean:
rm -f *.o *~ main
Makefile Dynamic
main: main.o libutil.so
g++ main.o -I. -o main -L. -lutil
main.o: libutil.so
g++ -fPIC -c -I. main.c
libutil.so: utilfile.o
g++ -shared utilfile.o -o libutil.so
utilfile.o: utilfile.c utilfile.h
g++ -fPIC -c -I. utilfile.c
clean:
rm -f *.o *~ main *.so
Run dynamic library: .so file is located in "/root/Cpp3"
[root@localhost Cpp3]# export LD_LIBRARY_PATH="/root/Cpp3"
[root@localhost Cpp3]# ./main