简体   繁体   中英

Can't make out what's wrong with my Makefile?

So, first off, I'd like to make clear that I've next to no experience with Make and it's Makefiles. That being said, I've tried finding all over the internet about wildcards and recursive techniques to compile and link my project. That is until I found a thread on this site with a modular type system using make.

I'd like to show you guys how my project is structured first:

.
|---- Makefile
|
|---- build/
|       |---- bin/
|       |
|       |____ obj/
|
|---- includes/
|         |
|         |____ graphics/
|                   |____ vga.h
|
|---- libs/
|       |____ linker.ld
|
|____ src/
       |---- module.mk
       |
       |---- asm/
       |      |---- asm_module.mk
       |      |____ boot.s
       |
       |____ cpp/
              |---- cpp_module.mk
              |---- main/
              |       |---- main_module.mk
              |       |____ main.cpp
              |
              |____ graphics/
                      |---- graphics_module.mk
                      |____ vga.cpp

So now you know this. I made the following Makefile and module.mk's to attempt to make the project:

Makefile:

PROJECT := GOODY

CXX := i686-elf-g++
LD  := i686-elf-ld
AS  := nasm

SRC_DIR := src
BUILD_DIR := build
OBJ_DIR := $(BUILD_DIR)/obj
BIN_DIR := $(BUILD_DIR)/bin

CXX_SRCS :=
ASM_SRCS :=
SUBDIRS :=
CXXFLAGS :=
LDFLAGS :=

include $(SRC_DIR)/module.mk

CXX_OBJS := $(addprefix $(OBJ_DIR)/, $(CXX_SRCS:.cpp=.o))
ASM_OBJS := $(addprefix $(OBJ_DIR)/, $(ASM_SRCS:.s=.o))

OBJS := $(CXX_OBJS) $(ASM_OBJS)

DEPS := $(OBJS:.o=.d)

TMPS := $(OBJS) $(OBJS:.o=.d)

CXXFLAGS := -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector \
    -fno-rtti -O2 -Wall -Werror -fno-exceptions -ffreestanding \
    -m32 -Iincludes
LDFLAGS := -Tlibs/linker.ld -melf_i386 --oformat=elf32-i386
ASFLAGS := -felf

all: $(BIN_DIR)/$(PROJECT)

$(OBJ_DIR)/%.o, $(SRC_DIR)/%.cpp
    @echo "\tCompiling [$@]"
    $(CXX) $(CXXFLAGS) $< -o $@

$(OBJ_DIR)/%.o, $(SRC_DIR)/%.s
    @echo "\tCompiling [$@]"
    $(AS) $(ASFLAGS) $< -o $@

$(BIN_DIR)/$(PROJECT): $(OBJS)
    @echo "\tLinking Kernel..."
    @$(LD) $(OBJS) -o $(PROJECT) $(LDFLAGS)

$(OBJS): | $(OBJ_DIR)

$(OBJ_DIR):
    @mkdir -p $(OBJ_DIR)
    @for dir in $(SUBDIRS); \
    do \
         mkdir -p $(OBJ_DIR)/$$dir; \
    done

clean:
    rm -rf $(TMPS)
    rm -rf $(BIN_DIR)/$(PROJECT)
    rm -rf $(OBJ_DIR)

.PHONY: clean

module.mk:

SUBDIRS += asm cpp

include $(SRC_DIR)/asm/asm_module.mk
include $(SRC_DIR)/cpp/cpp_module.mk

asm_module.mk:

UP_DIR := asm

MOD_SRCS := boot.s

ASM_SRCS := $(addprefix $(UP_DIR)/, $(MOD_SRCS))

cpp_module.mk:

SUBDIRS := main graphics

include $(SRC_DIR)/cpp/main/main_module.mk
include $(SRC_DIR)/cpp/graphics/graphics_module.mk

main_module.mk:

UP_DIR := main

MOD_SRCS := main.cpp

CXX_SRCS := $(addprefix $(UP_DIR)/, $(MOD_SRCS))

graphics_module.mk:

UP_DIR := graphics

MOD_SRCS := vga.cpp

CXX_SRCS := $(addprefix $(UP_DIR)/, $(MOD_SRCS))

Now to the error that I'm getting from make. It's as follows:

$ make
    Linking Kernel...
i686-elf-ld: cannot find build/obj/graphics/vga.o: No such file or directory
i686-elf-ld: cannot find build/obj/asm/boot.o: No such file or directory
Makefile:49: recipe for target 'build/bin/GOODY' failed
make: *** [build/bin/GOODY] Error 1

Now I can see from that, that the ld cannot find the .o files... and after looking in the build/obj directory, there's nothing in there.

My question is, what am I doing wrong?

EDIT:= So it looks like it's not actually compiling the *.cpp or *.s files at all, it's just skipping straight to the linking stage and obviously not finding anything to link against.

So at a guess it might also be something to do with:

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
    $(CXX) $(CXXFLAGS) $< -o $@

I've also tried what the original thread suggested which was:

$(COMPILE.cpp) $(OUTPUT_OPTION) $< # Also $(COMPILE.c) too.

So after leaving my laptop to it's own devices for a little while, I came back to it and realised I did a few things wrong later on down the line.

I needed to have the *.mk files have this instead:

module.mk:

DIR_SRC := src

SUBDIRS := cpp asm

include $(DIR_SRC)/cpp/cpp_module.mk
include $(DIR_SRC)/asm/asm_module.mk

cpp_module.mk:

DIR_CPP := cpp

SUBDIRS := main graphics

include $(SRC_DIR)/$(DIR_CPP)/main/main_module.mk
include $(SRC_DIR)/$(DIR_CPP)/graphics/graphics_module.mk

main.module.mk: (NOTE: This is pretty much the same with graphics_module.mk)

DIR_MAIN := main

MOD_SRCS := main.cpp

CXX_SRCS    := $(addprefix $(DIR_CPP)/$(DIR_MAIN)/, $(MOD_SRCS))

And that fixed my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM