1、静态库libtools.a源码 

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;
}
2、动态库libutility.so 源码

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;
}
3、主程序源码

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;
}
4、makefile
.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
5、测试

编译前文件构成:

makefile 编译多个目标_#include

编译后文件构成: 

makefile 编译多个目标_C++_02

 执行结果:

makefile 编译多个目标_#ifndef_03