Library makefile

I need to run these 4 commands on the terminal every time I want to execute a program using libraries.

Lines

cc -m32 -c mylib.c ar -rcs libmylib.a mylib.o cc -m32 -c prog.c cc -m32 prog.o -L. -lmylib ./a.out 

How to make a file for the above commands and run it? Detailed procedure will be appreciated. Thanks.


Edit: 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 

This gave an error on line 2 because I used spaces instead of tabs.

+8
c makefile static-libraries
source share
3 answers

Something like:

 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 

could start

I just started using make files myself, and I think they are quite complicated, but as soon as you earn them, they make life much easier (these problems are full of errors, but some of the more experienced SO people will have the opportunity to help fix them)

As for launching, make sure you save the file as a β€œMakefile” (case is important)

then from the cmd line (make sure you cd in the directory containing the Makefile):

 $ make 

here it is!

UPDATE

If the intermediate static library is redundant, you can skip it using the Makefile as follows:

 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) 
+7
source share

The simplest tutorial for understanding make files is available at Cprogramming.com . Once you are done understanding this, you can go with the make file manual.

+1
source share

I think there is no more detailed procedure than the official documentation of the make command: http://www.gnu.org/software/make/manual/make.html#Overview

Basically you will need to create a goal and just put your teams in it. The target can be "everything" if you want it to work when you type "make". A good makefile will certainly use variables, etc., to keep it flexible compared to adding lib / sources.

0
source share

All Articles