未定义参考主LD [英] Undefined reference to main ld

查看:338
本文介绍了未定义参考主LD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想链接到文件 - 包含主要的功能和简单的跳转到主的ASM文件中的C文件

I'm trying to link to files - a c file containing a main function and an asm file that simply jumps to the main.

我MinGW的安装。
我的文件:

I have mingw installed. My files:

//kernel.c
void some_function(){
}
void main(){
char* video_memory = (char*) 0xb8000;
*video_memory = 'X';
some_function();
}

;kernel_entry.asm
[bits 32]
[extern main]
call main
jmp $

命令我呼吁建:

gcc -ffreestanding -c kernel.c -o kernel.o
nasm kernel_entry.asm -f elf -o kernel_entry.o
ld -o kernel.bin -Ttext 0x1000 kernel_entry.o kernel.o

我得到的错误:

kernel_entry.o:(.text+0x1): undefined reference to `main'
kernel.o:kernel.c:(.text+0xf): undefined reference to `__main'

编辑:

哪些命令的工作:

ld -r -o kernel.out -Ttext 0x1000 kernel.o
objcopy -O binary -j .text kernel.out kernel.bin

当我尝试运行使用ld -r我得到一个错误:

When I try to run ld with -r i get an error:

ld: Relocatable linking with relocations from format elf32-i386 (kernel_entry.o)
 to format pe-i386 (kernel.bin) is not supported

EDIT2:
我使用这些命令时有最好的结果:

I had the best results when using these commands:

gcc -ffreestanding -c kernel.c -o kernel.o
nasm kernel_entry.asm -f win32 -o kernel_entry.o
ld -r -o kernel.out -Ttext 0x1000 kernel_entry.o kernel.o
objcopy -O binary -j .text kernel.out kernel.bin

该文件成功地联系在一起,但在运行时,主不会被调用。
另外随着COFF格式试过,还链接,但是Bochs的不断重新启动。

The files linked succesfully, but when running, the main never gets called. Also tried with coff format, it also links, but Bochs keeps restarting.

推荐答案

第一个错误是因为在C函数被命名为 _name ,所以你不能叫正因为如此,你必须调用 _main 。在TASM可以设置调用约定,所以assmebler自动可以调用正确的函数,我不知道是否有对NASM这样的功能也是如此。

The first error is is because in C a function is named _name, so you can not call main as such, you must call _main. In TASM you can set the calling convention, so the assmebler automatically can call the right function, I don't know if there is such a feature for nasm as well.

第二个问题是可能的,因为你是直接调用链接器。在这种情况下,你必须指定C启动模块,它通常是由编译器添加到链接器选项。通常我认为这是一个类似信息crt0 文件命名。如果你写你自己的启动code,你必须提供自己动手。这个模块设置为从OS特定条目点C处的环境。我想这是在你的项目中丢失了。

The second problem is probably, because you are calling the linker directly. In this case you must specify the C startup module, which is normally added by the compiler to the linker options. Usually I think this is a file named something like crt0. If you write your own startup code, you must provide this yourself. This module sets up the environment for C from the OS specific entry point. I guess this is missing in your project.

http://en.wikipedia.org/wiki/Crt0

这篇关于未定义参考主LD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆