netcat
netcat

Reputation: 1335

Set Option in MakeFile to send object files to specific Folder

I made a test makefile using an online tutorial. It works, but I also want to have all of my .o files go to a specific sub-folder. Is there a way to do this using a makefile? Here is what I have so far.

CC=gcc # specifies the compiler to use.

CFLAGS=-I. # specifies to look in the current directory.

DEPS = path_tools.h # DEPS stores each .h file to be added.

OBJ = checkpath.o path_tools.o # OBJ stores each object file to compile.

%.o: %.c $(DEPS)

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

checkpath.exe: $(OBJ)

        gcc -o $@ $^ $(CFLAGS)

Upvotes: 0

Views: 185

Answers (2)

Roman Bronshtein
Roman Bronshtein

Reputation: 578

For GNU make you can use this Makefile.


    ODIR:=obj
    CC:=gcc
    CFLAGS:=-I.
    DEPS:=path_tools.h
    OBJ_:= checkpath.o path_tools.o
    OBJ:=$(addprefix $(ODIR)/, $(OBJ_))
    PROG=checkpath.exe
    all:$(PROG)
    $(OBJ): $(DEPS)
    $(ODIR)/%.o: %.c 
        $(CC) -c -o $@ $< $(CFLAGS)
    $(PROG): $(OBJ)
        $(CC)  -o $@ $^ $(CFLAGS)
    clean:
        rm -f $(OBJ)  $(PROG)

Upvotes: 1

G. Emadi
G. Emadi

Reputation: 230

You can pass the path of your folder to makefile to create and put the results in. To pass parameter to makefile:

make DPATH=your-path

To use in makefile:

$(DPATH)

Create this path and add it to head of your *.o files as a path.

Upvotes: 1

Related Questions