首页 > 解决方案 > Ld Undefined reference to * 当从程序集中调用 c++ 函数时

问题描述

我有这个汇编代码

主程序

[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
}

生成文件

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/*

但是当我运行“make build”时,我得到了这个错误

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

那么如何使它能够从 main.asm 文件中调用汇编函数call foo_main而不会出现此错误

标签: c++assembly

解决方案


推荐阅读