0

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
Alvin CHENG
  • 374
  • 2
  • 16

1 Answers1

0

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.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • I wanted it to compile the kernel into a object file and later in linker I will create a binary and link it with the bootloader – Alvin CHENG Apr 20 '22 at 02:21
  • 1
    In general, you can't do that. An object file is typically the result of compiling a single source file. You create one object file per source file. If you really want to compile all the source files into a single object file, then the only way to do it is make another source file that contains `#include "foo.c"` `#include "bar.c"` etc. for all the `.c` files, then compile that one source file. However, this compilation will almost certainly fail unless the source code expects it, because in C the boundary of a source file is very important. – MadScientist Apr 20 '22 at 02:32
  • 1
    Maybe this will help: https://stackoverflow.com/questions/18182176/compile-multiple-c-source-fles-into-a-unique-object-file – MadScientist Apr 20 '22 at 02:33
  • I'll try thanks! – Alvin CHENG Apr 20 '22 at 02:41