文件目录结构如下
dynamiclibapp.c
Makefile
comm/inc/apue.h
comm/errorhandle.c
dynamiclib/Makefile
dynamiclib/dynamiclib_add.c
dynamiclib/dynamiclib_mul.c
dynamiclib/inc/dynamiclibs.h
dynamiclib/libs/
dynamiclib目录
dynamiclib/inc/dynamiclibs.h 文件内容如下:
#ifndef __dynamic_libs_h__
#define __dynamic_libs_h__
#include "apue.h"
int dynamic_lib_func_add(int i1, int i2);
int dynamic_lib_func_mul(int i1, int i2);
#endif
dynamiclib/dynamiclib_add.c 文件内容如下:
#include "dynamiclibs.h"
int dynamic_lib_func_add(int i1, int i2)
{
int iret = i1 + i2;
printf("... in .so func, %d add %d,return %d\n", i1, i2, iret);
return iret;
}
复制代码
dynamiclib/dynamiclib_mul.c 文件内容如下:
#include "dynamiclibs.h"
int dynamic_lib_func_mul(int i1, int i2)
{
int iret = i1 * i2;
printf("... in .so func, %d multiplys %d, retun %d\n", i1, i2, iret);
return iret;
}
dynamiclib/Makefile 文件内容如下:
1 CC = gcc
2 CFLAGS = -Wall -g -O -fPIC 需要加上 -fPIC
3 CXXFLAGS =
4 INCLUDE = -I ./inc -I …/comm/inc
5 TARGET = libmytest.so
6 LIBPATH = ./libs/
7
8 vpath %.h ./inc
9
10 OBJS = dynamiclib_add.o dynamiclib_mul.o
11 SRCS = dynamiclib_add.c dynamiclib_mul.c
12
13 ( O B J S ) : (OBJS): (OBJS):