当使用ld链接时,未定义的引用'__main' [英] When using ld to link, undefined reference to '__main'
问题描述
/* test.c */
void func1()
{
}
int main()
{
func1();
}
你好,我正在用C编制内核代码。知道如何构建C内核代码。下面的命令是我给的提示。我在Windows 8.1上使用MinGW。
Hello, I am making kernel code using C. But I tested above code to know how to build C kernel code. Below command is what I gave to prompt. I am using MinGW on Windows 8.1.
gcc -c -m32 test.c
ld -o test -Ttext 0x00 -e _main test.o
但是这个错误发生在ld。
But this error was occurred from ld.
test.o:test.c:(.text+0x7): undefined reference to `__main'
所以,我尝试了不同的方式。添加-nostdlib和 - gcc的免费选项。但结果是一样的。 CRT0中的__main函数是什么?我应该怎么做才能解决这个问题?
So, I tried different way. add -nostdlib and --freestanding option to gcc. But the result was same. Is __main function in CRT0 ? What should I do to solve this problem.. ?
推荐答案
如果你真的进入操作系统开发的唯一可行的方法是通过使用像GNU / Linux或Mac OS X的类Unix操作系统。
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.
以下两个是必须的:
-ffreestanding -nostdlib -lgcc
然后像建议 -Wextra
和 -Werror
因为内核代码中的错误非常难以调试。
Then things like -Wall
, -Wextra
, and -Werror
are recommended because bugs in kernel code are extremely hard to debug.
关于入口点,通常使用链接器脚本,通过 -T传递给
。例如,我的(不要复制粘贴它!)如下所示。它用于支持虚拟内存的: ld
linker.ld
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__ = .;
}
这篇关于当使用ld链接时,未定义的引用'__main'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!