gilgamesh
gilgamesh

Reputation: 462

Makefile: Compiling without Linking Issue

Currently I'm trying to compile .c-files in subfolder(s) without linking and created a simple draft in Makefile. What I want to do is take .o files into bin/ directory without linking as I described before. However couldn't reach any output successfully.

There is sample Makefile:

IDIR = test/inc
ODIR = test/src
CC = gcc
CFLAGS = -std=c99 -I$(IDIR)

OBJDIR    =   bin

.PHONY: clean

$(OBJDIR):
    mkdir   $(OBJDIR)

$(OBJDIR)/%.o:  $(ODIR)/*.c
    $(CC)   $(CFLAGS)   -c  $<  -o  $@

clean:
    @rm -rf $(OBJDIR)

It would be great if you define what did I missed.

Upvotes: 0

Views: 925

Answers (1)

user3629249
user3629249

Reputation: 16540

the make file needs to actually ask for all the object files to be created. Suggest:

IDIR := test/inc
SDIR := test/src/

CC := gcc
CFLAGS := -Wall -Wextra -Wconversion -pedantic -std=c99 -I$(IDIR)

OBJDIR    :=   bin/

SRCS := (wildcard:$(SDIR)*.c)
OBJS := ($(SRCS):.c=.o)

.PHONY: all clean

all: $(OBJDIR)  $(OBJS)


$(OBJDIR):
    mkdir   $(OBJDIR)

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

clean:
    @rm -rf $(OBJDIR)

run the above makefile using:

make -f (your make file name) all

Upvotes: 2

Related Questions