0

I have this assembmly code

main.asm

[bits 32]
global _start
extern foo_main

section .mbHeader
    align 0x4
    MODULEALIGN equ  1<<0
    MEMINFO     equ  1<<1
    FLAGS       equ  MODULEALIGN | MEMINFO
    MAGIC       equ  0x1BADB002
    CHECKSUM    equ -(MAGIC + FLAGS)

MultiBootHeader:
   dd MAGIC
   dd FLAGS
   dd CHECKSUM

_start:
    push ebx
    call foo_main

foo.cpp

void foo_main()
{
   // stuff
}

Makefile

foo_source_files := $(shell find . -name *.cpp)
foo_object_files := $(patsubst %.cpp, build/%.o, $(foo_source_files))

asm_source_files := $(shell find . -name *.asm)
asm_object_files := $(patsubst ./%.asm, build/%.o, $(asm_source_files))

$(foo_object_files): ./build/%.o : ./%.cpp
    mkdir -p $(dir $@) && \
    g++ -m32 -c $(foo_source_files) -o $@ -ffreestanding -O2 -Wall -Wextra

$(asm_object_files): ./build/boot/%.o : ./boot/%.asm
    mkdir -p $(dir $@) && \
    nasm -f elf $(asm_source_files) -o $@

.PHONY: build
build: $(foo_object_files) $(asm_object_files)
    mkdir -p ./dist && \
    ld -m elf_i386 -T ./targets/linker.ld $(kernel_object_files) $(asm_object_files) -o MyOS.bin -nostdlib

.PHONY: clear
clear:
    rm -rf ./build/*

But when I run "make build" I get this error

ld: build/boot.o: in function `_start':
./boot/main.asm:(.mbHeader+0xe): undefined reference to `foo_main'
make: *** [Makefile:19: build] Error 1

So how would I make it able to call the assembly function from the main.asm file with call foo_mainwithout getting this error

Robiot
  • 98
  • 10
  • 2
    Is it `foo.c` or `foo.cpp`? Makes a big difference in your setup. If C++, declare `foo_main` as `extern "C"`? – Nate Eldredge Jun 18 '21 at 19:57
  • Is foo.c really foo.cpp? Your makefile has no reference to gcc or .c files. – doron Jun 18 '21 at 19:58
  • `g++` may compile as C++ (with name mangling), even if you run it on a `.c` file. Check with `nm` on `foo.o` – Peter Cordes Jun 18 '21 at 19:58
  • I accidently typed foo.c. I meant foo.cpp. Thanks for noticing that! – Robiot Jun 18 '21 at 20:00
  • @AlanBirtles: The duplicate you picked is not very specific. The relevant answer (about `extern "C"`) is several answers down in [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/a/12574420). To be fair, that point was already mentioned in comments, but I think there's an asm-specific Q&A about C++ name mangling somewhere. – Peter Cordes Jun 19 '21 at 11:11

0 Answers0