libtools.h
#ifndef tools_h_ #define tools_h_ int sub(int x,int y); int mul(int x,int y); #endif
libtools.cpp
#include"libtools.h" int sub(int x,int y) { return x - y; } int mul(int x ,int y) { return x * y; }
libutility.h
#ifndef utility_h_ #define utility_h_ int div(int x,int y); #endif
libutility.cpp
#include"libutility.h" int div(int x,int y) { return x/y; }
main.cpp
#include "libtools.h" #include "libutility.h" #include <stdio.h> int main() { printf("sub(10,7)=%d\n",sub(10,7)); printf("mul(3,3)=%d\n",mul(3,3)); printf("div(3,3)=%d\n",div(3,3)); return 0; }
.PHONY:clean all : libtools.a libutility.so hello libtools.a: libtools.o ar rcs $@ $^ libtools.o:libtools.cpp libtools.h gcc -o $@ -c libtools.cpp libutility.so: libutility.o ld -shared -o $@ $^ libutility.o: libutility.cpp libutility.h gcc -o $@ -c libutility.cpp hello:main.o gcc -o $@ $^ -L./ -ltools -lutility main.o: main.cpp gcc -o $@ -c $^ clean: rm hello main.o libtools.a libtools.o libutility.so libutility.o
编译前文件构成:
编译后文件构成:
执行结果: