1.文件结构
hello (用于生产动态库的文件)
+ hello.c hello.h Makefile
lib(用于存放编译好的动态库)
test.c test Makefile
2.相关代码
hello.h
#ifndef _HELLO_H_
#define _HELLO_H_
#define debug_c 1
#ifdef debug_c
#define MSGT "msg is good \n"
#else
#define MSGT "msg is bye \n"
#endif
void debug(void);
#endif
hello.c
#include <stdio.h>
#include "hello.h"
void debug(void)
{
printf(MSGT);
}
###############################################################
#
# Purpose: Makefile for hello
# Author.: Angel
# Version: 1.0
# License: GPL
#
###############################################################
CC = gcc
OTHER_HEADERS =./hello.h
CFLAGS += -O2 -Wall -shared -fPIC
LFLAGS += -ldl
filename = hello.c
all: libhello.so
clean:
rm -f *.a *.o core *~ *.so *.lo
libhello.so: $(OTHER_HEADERS) $(filename)
$(CC) $(CFLAGS) -o $@ $(filename)
test.c
#include "hello.h"
int main(void)
{
debug();
}
Makefile
################################################################
# Purpose: Makefile for test
# Author.: angel
# Version: 1.0
# License: GPL
#
###############################################################
CC = gcc
OTHER_HEADERS =
CFLAGS += -O2 -Wall -l hello -L ./lib -I ./hello
LFLAGS += -ldl
all:libhello.so test
libhello.so:
make -C ./hello/ all
cp ./hello/libhello.so ./lib
test:$(OTHER_HEADERS) test.c
$(CC) $(CFLAGS) -o $@ test.c
export LD_LIBRARY_PATH=$(shell pwd)/lib
clean:
make -C ./hello $@
rm -f *.a *.o core *~ *.so *.lo test
rm -f ./lib/*