makefile编写---.a静态库的生成和调用


#.SUFFIXES: .c .o

Cc =gcc

#OSA=/data/users/osa

IncDir=-I. -I./

Debug = -g

Cflags = -c $(DEBUG)

Libs = -lpthread
Lib_Dir=./
#/data/users/osa/api/

#PKIOBJ=PKITool.o kdmd5.o

Lib=alg.a
PkiObj=alg.o

.c.o:
 $(Cc) $(Cflags) $(IncDir) $*.c

all: PKITool
#clean

PKITool:$(PkiObj)
 ar ruv $(Lib_Dir)$(Lib) *.o
 ranlib $(Lib_Dir)$(Lib)

clean:
 rm *.o

 

target=testmake
cc  = gcc
objects  = main.o
#mult.o sub.o
link =
cflags  =  Wall -O
lflags =  -lpthread
ar = ar
includes = -l
includelib =./alg.a

.PHONY:clean

all:$(target)

$(target):$(objects)
 $(cc) $^ -o $@  $(lflag) $(includelib)
#$(objects) == $^
#edit:$(objects)
# $(cc)  -o edit  $(objects)

#$(TARGET): $(OBJS)
#     $(CC) $^ -o $@ $(INCLUDE) $(LFLAGS)


main.o:main.c alg.h
 $(cc) -c main.c

clean:
 -rm *.o $(target)

 

这里的main调用库不能以系统文件形式包含

 

 

/***********************以上是ok的********************************/

bug1:

CLib =  -L. inilib.a  //这里没加-l

$(Target): $(Cobjects)
$(GCC) $(CLib) -o $@ $^

上面的顺序会导致错误如下

gcc -L. inilib.a -o TestIni main.o
main.o: In function `main':
main.c:(.text+0x35): undefined reference to `iniParser_init'
main.c:(.text+0x154): undefined reference to `iniParser_getString'
main.c:(.text+0x1b4): undefined reference to `iniParser_setString'
main.c:(.text+0x1c9): undefined reference to `iniParser_uinit'

solve1:

是依赖库.a 放错位置

$(Target): $(Cobjects)
$(GCC) -o $@ $^ $(CLib) 这样就可以通过

 

bug2:

CLib =  -L. -linilib.a

$(Target): $(Cobjects)
$(GCC) -o $@ $^ $(CLib) //

root@ubuntu:/opt/54/app/ini/inifileok/iniapi# make -f Makefiles all
gcc -L. -linilib.a -o TestIni main.o
/usr/bin/ld: cannot find -linilib.a
collect2: ld returned 1 exit status
make: *** [TestIni] 错误 1

solve2:

CLib =  -L. inilib.a //去掉-l 而且这里-L. 就是表示在当前目录加载路径

 

下面的静态库生成 ,调用是可以的

create:

 


Target = TestIni
TargetLib = inilib.a
GCC = gcc
G++ = g++

CInc = $(wildcard *.h)
CSrc = $(wildcard *.c)

Cobjects =$(CSrc:%.c=%.o)
#Cobjects =$(patsubst %.c,%.o,$(CSrc))

lib:$(TargetLib)

release:$(Target)

$(Target): $(Cobjects)
$(GCC) -o $@ $^


$(TargetLib): $(Cobjects)
ar ruv $(TargetLib) $^
# ar rcs $(TargetLib) $^


##################test######################

#CInc += iniparser.h dictionary.h
#CSrc += iniparser.c dictionary.c
#Cobjects += iniparser.o dictionary.o
#CppObejects =
#$(Cobjects):$(Csrc)
# $(GCC) $(CInc) -c $<

#$(Target): iniparser.o dictionary.o
# $(GCC) -o $@ $^

#iniparser.o: iniparser.c
# $(GCC) $(CInc) -c $<
#dictionary.o: dictionary.c
# $(GCC) $(CInc) -c $<


clean:
rm -rf *.o $(Target) *.gch

load:
Target = TestIni
TargetLib = ./inilib.a
GCC = gcc
G++ = g++

CInc = $(wildcard *.h)
CSrc = $(wildcard *.c)

CInc = -I ./
CLib = -L. inilib.a
Cobjects =$(CSrc:%.c=%.o)
#Cobjects =$(patsubst %.c,%.o,$(CSrc))

all:release
lib:$(TargetLib)

release:$(Target)

$(Target): $(Cobjects)
$(GCC) -o $@ $^ $(CLib)

#$(Target):$(Cobjects)
# $(GCC) $^ -o $@ $(TargetLib)


##################test######################

#CInc += iniparser.h dictionary.h
#CSrc += iniparser.c dictionary.c
#Cobjects += iniparser.o dictionary.o
#CppObejects =
#$(Cobjects):$(Csrc)
# $(GCC) $(CInc) -c $<

#$(Target): iniparser.o dictionary.o
# $(GCC) -o $@ $^

#iniparser.o: iniparser.c
# $(GCC) $(CInc) -c $<
#dictionary.o: dictionary.c
# $(GCC) $(CInc) -c $<


clean:
rm -rf *.o $(Target) *.gch

你可能感兴趣的:(makefile编写---.a静态库的生成和调用)