fat_flying_pigs fat_flying_pigs - 3 months ago 5
C++ Question

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:


  • Changing
    $(BINDER)/$(TARGET):
    to
    $(BINDER)
    (
    make: bin: No such file or directory
    )

  • Changing folder permissions and running as sudo (no effect)

  • Making the bin/kernel folder (Permission Denied w/ sudo and chmod 777)

  • Praying to our dark lord Cthulhu (possible inevitable doom)


Answer

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