fat_flying_pigs
fat_flying_pigs

Reputation: 90

Error with makefile, building from all in src directory

I have been attempting to replicate a makefile I found online. I am currently stuck at the following error:

MyComputer:folderName userName$ make
make: bin/kernel: No such file or directory
make: *** [bin/kernel] Error 1

The makefile source is as follows:

### Typing 'make' will create the executable file.
CC = gcc
CFLAGS  = -g -Wall
LDFLAGS = 
LIBS = -lstdc++

TARGET = kernel
SRCDIR = src
OBJDIR = obj
BINDIR = bin
#TARGETDIR = $(BINDIR)/$(TARGET)

SOURCES  := $(wildcard $(SRCDIR)/*.cc)
INCLUDES := $(wildcard $(SRCDIR)/*.h)
OBJECTS  := $(SOURCES:$(SRCDIR)/%.cc=$(OBJDIR)/%.o)
rm       = rm -f

$(BINDIR)/$(TARGET): $(OBJECTS)
    @$(LINKER) $@ $(LFLAGS) $(OBJECTS) $(LIBS)
    @echo "Linking complete!"

$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.cc
    @$(CC) $(CFLAGS) -c $< -o $@
    @echo "Compiled "$<" successfully!"

#$(CC) $(CFLAGS) $(LFLAGS) -o $(MAIN) $(OBJS) $(LIBS) $@

#make clean removed from this code, unnecessary

Things I have tried:

Upvotes: 0

Views: 169

Answers (1)

Trevor Hickey
Trevor Hickey

Reputation: 37834

Your Makefile assumes the project directory is structured in the following way:

.
├── bin
├── Makefile
├── obj
└── src
    └── main.cc

I modified the linking phase as you did not have $(LINKER) defined:

CC = gcc
CFLAGS  = -g -Wall
LDFLAGS = 
LIBS = -lstdc++

TARGET = kernel
SRCDIR = src
OBJDIR = obj
BINDIR = bin

SOURCES  := $(wildcard $(SRCDIR)/*.cc)
INCLUDES := $(wildcard $(SRCDIR)/*.h)
OBJECTS  := $(SOURCES:$(SRCDIR)/%.cc=$(OBJDIR)/%.o)
rm       = rm -f

$(BINDIR)/$(TARGET): $(OBJECTS)
    @$(CC) -o $@ $(LFLAGS) $(OBJECTS) $(LIBS)
    @echo "Linking complete!"

$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.cc
    @$(CC) $(CFLAGS) -c $< -o $@
    @echo "Compiled "$<" successfully!" 

running make will now create the kernel binary.

.
├── bin
│   └── kernel
├── Makefile
├── obj
│   └── main.o
└── src
    └── main.cc

Upvotes: 2

Related Questions