源码目录结构
code目录
calc.c
#include<calc.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
if(argc!=3){
printf("parameter error!");
return -1;
}
int a=atoi(argv[1]);
int b=atoi(argv[2]);
printf("%d + %d = %d\n",a,b,add(a,b));
printf("%d - %d = %d\n",a,b,sub(a,b));
printf("%d * %d = %d\n",a,b,mult(a,b));
printf("%d / %d = %.2f\n",a,b,mydiv(a,b));
return 0;
}
Makefile
ARCH :=arm
CROS_COMPILE :=
Target :=build
OBJ :=calc.c
CC :=gcc
CFLAGS := -I ../include -o
LDFLAGS :=-L ../lib -lshare -Wl,rpath='./'
.PHONY:all
all:
$(CROS_COMPILE)$(CC) $(CFLAGS) $(Target) $(OBJ) $(LDFLAGS)
.PHONY:clean
clean:
rm $(Target)
include目录
calc.h
#ifndef __CALC_H_
#define __CALC_H_
#include<stdio.h>
extern int add(int a,int b);
extern int sub(int a,int b);
extern int mult(int a,int b);
extern float mydiv(int a,int b);
#endif
lib目录
add.c
#include<calc.h>
int add(int a,int b)
{
return a+b;
}
sub.c
#include<calc.h>
int sub(int a,int b)
{
return a-b;
}
mult.c
#include<calc.h>
int mult(int a,int b)
{
return a*b;
}
div.c
#include<calc.h>
float mydiv(int a,int b)
{
return (float)a/b;
}
Makefile
Target_share :=libshare.so
#Target_static :=libstatic.a
# if you want create a static library ,you can search in google browser(bing)
OBJ := add.c div.c mult.c sub.c
CC :=gcc
CFLAGS :=-I ../include/ -fPIC -shared -o
.PHONY:all
all:
$(CC) $(CFLAGS) $(Target_share) $(OBJ)
.PHONY:clean
clean:
rm -f $(Target_share)
测试验证
首先制作生成动态库,在lib目录下执行make
回到code目录执行make
好的,没有问题。