some_id
some_id

Reputation: 29886

Creating a make file

How would one create a make file for running e.g. xml parser with the following command line command

gcc source.c -I/usr/include/libxml2 -lxml2 -o output

I use this to include libxml when compiling using the command line.

How would this be made using a make file instead of the command line usage?

Upvotes: 0

Views: 272

Answers (3)

Erik
Erik

Reputation: 91260

SOURCES:=source.c
OBJECTS:=$(SOURCES:%.c=%.o)
CC=gcc
CFLAGS=-g -Wall -I/usr/include/libxml2
LD=gcc
LDFLAGS=
LIBS=-lxml2
TARGET:=output



all: $(TARGET)

$(TARGET): $(OBJECTS)
        $(LD) -o $@ $(OBJECTS) $(LDFLAGS) $(LIBS)

.c.o:
        $(CC) $(CFLAGS) -c $< -o $@

depend:
        $(CC) $(CFLAGS) -MM $(SOURCES) > .depend

clean:
        rm -f $(OBJECTS) $(TARGET) 

.depend: depend

include .depend

You can use this as a template, then add to/modify SOURCES, TARGET, CFLAGS, LDFLAGS and LIBS. Makefiles require TABs for indentation - so you'll need to fix the spaces if you copy-paste this.

Upvotes: 3

William Pursell
William Pursell

Reputation: 212148

If you allow yourself to have the output file be the same name as the source (without .c suffix), you can just do:

CPPFLAGS = -I/usr/include/libxml2
LDFLAGS = -lxml2

With just those two lines in your makefile,

make source

will look for source.c and generate the executable named 'source' with the appropriate flags.

Upvotes: 0

pajton
pajton

Reputation: 16226

In its simpliest form:

output: source.c
    gcc source.c -I/usr/include/libxml2 -lxml2 -o output

now you can say either make output of make and get output built.

Upvotes: 0

Related Questions