使用 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'
所以,我尝试了不同的方式.向 gcc 添加 -nostdlib 和 --freestanding 选项.但结果是一样的.__main 函数在 CRT0 中吗?我应该怎么做才能解决这个问题..?
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.. ?
推荐答案
如果您真正热衷于操作系统开发,唯一可行的方法是使用一些类 Unix 操作系统,例如 GNU/Linux 或 Mac OS X.
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
然后推荐使用 -Wall
、-Wextra
和 -Werror
之类的东西,因为内核代码中的错误非常难 调试.
Then things like -Wall
, -Wextra
, and -Werror
are recommended because bugs in kernel code are extremely hard to debug.
关于入口点,您通常使用传递给 的 链接器脚本ld
通过 -T linker.ld
.例如,我的(不要复制粘贴!)如下所示.它适用于支持虚拟内存的 higher-half kernel:
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屋!