'fatal error: cannot specify '-o' with '-c', '-S' or '-E' with multiple files compilation terminated

👋 I'm creating a os with c with many source files, but when I compile to source files I get a gcc error:

x86_64-elf-gcc: fatal error: cannot specify '-o' with '-c', '-S' or '-E' with multiple files
compilation terminated.

I used a make file and here is the makefile:

build_bootloader:
    echo "Building assembly..."
    ${ASM} -i ./src/bootloader/ ./src/bootloader/boot.asm -f bin -o ${BUILD_DIR}/boot.bin 
    echo "Done."

create_img:
    echo "Creating disk image..."
    touch ${BUILD_DIR}/disk.img
    cp ${BUILD_DIR}/boot.bin ${BUILD_DIR}/disk.img
    echo "Disk image created."

clean:
    rm -rf ${BUILD_DIR}
    mkdir ${BUILD_DIR}

build_kernel:
    echo "Building kernel..."
    ${ASM} ./src/kernel/kernel_entry.asm -f elf64 -o ${BUILD_DIR}/kernel_entry.o
    ${C_COMPILER} -ffreestanding -c ./src/kernel/*.h ./src/kernel/*.c ./src/kernel/*.h -o ${BUILD_DIR}/kernel.o
    echo "kernel build complete."

link:
    echo "Linking..."
    ${LINKER} -o ${BUILD_DIR}/kernel.bin -Ttext 0x1000 ${BUILD_DIR}/kernel.o ${BUILD_DIR}/kernel_entry.o --oformat binary
    echo "Linking complete"

run:
    echo "Running qemu..."
    qemu-system-x86_64 -fda ${BUILD_DIR}/os.bin

merge_binary:
    echo "Merging binary..."
    cat ${BUILD_DIR}/boot.bin ${BUILD_DIR}/kernel.bin > ${BUILD_DIR}/os.bin
    echo "Binary merged."

I've tried to search on stackoverflow and a couple random forms on the internet.

Any help will be appreciated!

EDIT: Here is the make logs:

rm -rf ./build
mkdir ./build
make build_bootloader
echo "Building assembly..."
Building assembly...
nasm -i ./src/bootloader/ ./src/bootloader/boot.asm -f bin -o ./build/boot.bin 
echo "Done."
Done.
make build_kernel
echo "Building kernel..."
Building kernel...
nasm ./src/kernel/kernel_entry.asm -f elf64 -o ./build/kernel_entry.o
x86_64-elf-gcc -ffreestanding -c ./src/kernel/*.c ./src/kernel/*.h -o ./build/kernel.o
x86_64-elf-gcc: fatal error: cannot specify '-o' with '-c', '-S' or '-E' with multiple files
compilation terminated.
make[1]: *** [build_kernel] Error 1
make: *** [all] Error 2


Solution 1:[1]

I'm not sure what you want this line to do, but it won't work:

${C_COMPILER} -ffreestanding -c ./src/kernel/*.h ./src/kernel/*.c ./src/kernel/*.h -o ${BUILD_DIR}/kernel.o

The -c plus -o options to the compiler says, "compile one source file and generate one object file". You have passed ALL the source files and ALL the header files (note, you don't usually add header files to compile lines) to the compiler, and "ALL" (in this case) is more than "one", so you get the error you see.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MadScientist