When using ld to link, undefined reference to '__main'

纵然是瞬间 提交于 2019-12-01 20:40:55
3442

The only viable way if you're really into operating system development is by using some Unix-like OS like GNU/Linux or Mac OS X.

The following two are a must:

-ffreestanding -nostdlib -lgcc

Then things like -Wall, -Wextra, and -Werror are recommended because bugs in kernel code are extremely hard to debug.

With respect to the entry point, you usually use a linker script that you pass to ld via -T linker.ld. For example, mine (don't copy paste it!) looks as follows. It's for a higher-half kernel with support for virtual memory:

ENTRY(__start__)
OUTPUT_FORMAT(elf32-i386)

SECTIONS {
    . = 0xC0100000;

    .text BLOCK(4K) : AT(ADDR(.text) - 0xC0000000) {
        KEEP(*(.multiboot))
        KEEP(*(.boot))
        *(.text)
    }

    .rodata ALIGN(0x1000) : AT(ADDR(.rodata) - 0xC0000000) {
        *(.rodata*)
    }

    .data ALIGN(0x1000) : AT(ADDR(.data) - 0xC0000000) {
        *(.data)
    }

    .bss : AT(ADDR(.bss) - 0xC0000000) {
        *(COMMON)
        *(.bss)
        *(.stack)
    }

    __kend__ = .;
}

You could use gcc instead of ld to perform the linking:

gcc -o test test.o -nostdlib -lgcc

The -lgcc option provides the __main function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!