makefile編寫 a靜態庫的生成和呼叫

2022-02-03 19:03:13 字數 3921 閱讀 5495

#.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) //

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鏈結靜態庫

先來看個例子 先寫一下實現加減功能的子函式。主函式要呼叫另乙個c檔案裡面的函式,一般是通過共同包含同乙個.件實現的。filename add minus.h ifndef add minus h define add minus h int add int a,int b int minus int...

靜態庫lib的編寫

最早在學習的時候,寫庫檔案比較少,所以有些記不清楚,今天寫下來以便以後的學習和複習。我寫乙個簡單的lib庫,正所謂知微見著,希望也能給大家帶來幫助。我使用的是vs2013編寫庫,首先建立乙個靜態庫的工程,如下圖所示 然後寫乙個標頭檔案命名為 lib.h 內容為 ifndef lib h define...

如何編寫靜態庫

1.使用vs2013建立普通的應用臺控制程式 2.在屬性裡選擇靜態庫 lib 編寫庫檔案 mylib.h pragam once int sum int a,int b include mylib.h 函式實現 int sum int a,int b 3.編譯,成功後會在目錄下生成.lib檔案4.使...