user2071938
user2071938

Reputation: 2265

simple Makefile for c program

I have this (working) Makefile for single-file C-Application.

all: simpleua

simpleua: simpleua.c
    $(CC) -o $@ $< `pkg-config --cflags --libs libpjproject`

now I want to extend this Makefile for a multiple-file C-Application. The files are main.c simpleua.c simpleua.h

I have found some samples on the Internet but nothing simple and working with pkg-config

Thanks alot florian

Upvotes: 0

Views: 281

Answers (2)

Beta
Beta

Reputation: 99172

This may take a few iterations (and you've left out a lot of details). Try this:

all: simpleua

simpleua: simpleua.o main.o
    $(CC) -o $@ $^ `pkg-config --cflags --libs libpjproject`

simpleua.o main.o: %.o: %.c simpleua.h
    $(CC) -c -o $@ $< `pkg-config --cflags --libs libproject`

EDIT: second try (with thanks to Jonathan Leffler). The use of --libs in the pattern rule may be unnecessary, but try it and see.

Upvotes: 1

Jayesh Bhoi
Jayesh Bhoi

Reputation: 25915

This is simple example Makefile for you

CC = gcc
XX = g++
CFLAGS = -g

INC := -I test.h

$(LIBS)

TGT = ./sample
OUTPUT = ../output/

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


SOURCES = $(wildcard *.c *.cpp)
OBJS = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCES)))

$(TGT) : $(OBJS)
    $(CC) $(OBJS) -o $(TGT)    
    chmod 777 $(TGT)
    cp $(TGT) $(OUTPUT)

clean:
    rm -rf *.o *~ $(OUTPUT)/* 

It works for both C and C++ for this you just need change from CC to XX. For example purpose i have two directory as in Makefile

1)sample 

2)output

In sample directory all my source resides and in output folder my final binary copied (you can give any name to directory as you want but make sure you also give same name in Makefile ).so you can put as many source file in sample directory and able to compile it.

Upvotes: 1

Related Questions