Makefile文件库 [英] Makefile for a library
问题描述
我有我想要执行使用库程序每次在终端上运行这些命令4。
I have to run these 4 commands on the terminal each time I want to execute the program using libraries.
该行
cc -m32 -c mylib.c
ar -rcs libmylib.a mylib.o
cc -m32 -c prog.c
cc -m32 prog.o -L. -lmylib
./a.out
我如何作出上述命令生成文件并运行它?
具体程序是AP preciated。谢谢你。
How do I make a makefile for the above commands and run it? A detailed procedure would be appreciated. Thanks.
编辑:
这里是解决方案:
Here is the solution:
a.out: prog.o libmylib.a
cc prog.o -L. -lmylib
prog.o: prog.c mylib.h
libprint_int.a: mylib.o
ar -rcs libmylib.a mylib.o
print_int.o: mylib.c mylib.h
clean:
rm a.out prog.o libmylib.a mylib.o
这给了一个错误就行2,因为我用的,而不是标签空间。
This gave an error on line 2 because I used spaces instead of tab.
推荐答案
是这样的:
program_NAME := a.out
SRCS = mylib.c prog.c
.PHONY: all
all: $(program_NAME)
$(program_NAME): $(SRCS)
ar -rcs libmylib.a mylib.o
cc -m32 prog.o -L. -lmylib
也许让你开始
才刚刚开始使用的makefile自己,我认为他们是pretty棘手,但一旦你让他们的工作,他们让生活变得更加简单(这那些概率完全错误的,但一些比较有经验的SO民间将概率能以帮助解决他们)
only just started using makefiles myself and I think they are pretty tricky but once you get them working they make life a lot easier (this ones prob full of bugs but some of the more experienced SO folk will prob be able to help fix them)
至于运行,请确保您的文件保存为'Makefile文件(大小写很重要)
As for running, make sure you save the file as 'Makefile' (case is important)
然后从CMD线(确保你cd到包含Makefile中的DIR):
then from the cmd line (ensure you cd to the dir containing the Makefile):
$ make
完蛋了!
的更新的
如果中间静态库是多余的,你可以用一个Makefile这样跳过它:
if the intermediate static library is superfluous you could skip it with a Makefile like this:
program_NAME := a.out
SRCS = mylib.c prog.c
OBJS := ${SRCS:.c=.o}
CFLAGS += -m32
program_INCLUDE_DIRS :=
program_LIBRARY_DIRS :=
program_LIBRARIES := mylib
CPPFLAGS += $(foreach includedir,$(program_INCLUDE_DIRS),-I$(includedir))
LDFLAGS += $(foreach librarydir,$(program_LIBRARY_DIRS),-L$(librarydir))
LDFLAGS += $(foreach library,$(program_LIBRARIES),-l$(library))
CC=cc
LINK.c := $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
.PHONY: all
all: $(program_NAME)
$(program_NAME): $(OBJS)
$(LINK.c) $(program_OBJS) -o $(program_NAME)
这篇关于Makefile文件库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!