链接器无法识别 extern 语句
我正在关注 osdever 上的内核开发教程,并且正在打印到屏幕部分教程的。它编译得很好,没有错误或警告,但是当它链接时,我得到:
screen.o: In function `scroll':
screen.c:(.text+0x12c): undefined reference to `memcpy'
screen.o: In function `put_str':
screen.c:(.text+0x249): undefined reference to `strlen'
我在 screen.c 的顶部确实有一个包含 system.h 的语句,并且在 system.h 中我
#ifndef __SYSTEM_H
#define __SYSTEM_H
extern unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count);
extern unsigned char *memset(unsigned char *dest, unsigned char val, int count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count);
extern int strlen(const char *str);
extern unsigned char inportb (unsigned short _port);
extern void outportb (unsigned short _port, unsigned char _data);
extern void clear();
extern void put_char(unsigned char c);
extern void put_str(unsigned char *str);
extern void set_color(unsigned char fcol, unsigned char bcol);
extern void init_video();
#endif
找不到网上有任何关于此的信息,并且教程中没有任何帮助。似乎当我更改链接器语句中的顺序时,从
/usr/local/cross/bin/i586-elf-ld -T link.ld -o kernel.bin start.o main.o screen.o
到
/usr/local/cross/bin/i586-elf-ld -T link.ld -o kernel.bin start.o screen.o main.o
我得到
screen.o: In function `put_str':
screen.c:(.text+0x249): undefined reference to `strlen'
main.o: In function `main':
main.c:(.text+0x9b): undefined reference to `init_video'
编辑 http://pastebin.com/ibBAVpe5 - Main.c
I'm following the kernel development tutorial at osdever, and I'm at the printing to the screen portion of the tutorial. It compiles just fine, no errors or warnings, but when it goes to link, I get:
screen.o: In function `scroll':
screen.c:(.text+0x12c): undefined reference to `memcpy'
screen.o: In function `put_str':
screen.c:(.text+0x249): undefined reference to `strlen'
I do have a include statement to system.h at the top of screen.c, and in system.h I have
#ifndef __SYSTEM_H
#define __SYSTEM_H
extern unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count);
extern unsigned char *memset(unsigned char *dest, unsigned char val, int count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count);
extern int strlen(const char *str);
extern unsigned char inportb (unsigned short _port);
extern void outportb (unsigned short _port, unsigned char _data);
extern void clear();
extern void put_char(unsigned char c);
extern void put_str(unsigned char *str);
extern void set_color(unsigned char fcol, unsigned char bcol);
extern void init_video();
#endif
I can't find anything about this on the web, and there's no help in the tutorial. It seems that when I change the order in the linker statement, from
/usr/local/cross/bin/i586-elf-ld -T link.ld -o kernel.bin start.o main.o screen.o
to
/usr/local/cross/bin/i586-elf-ld -T link.ld -o kernel.bin start.o screen.o main.o
I get
screen.o: In function `put_str':
screen.c:(.text+0x249): undefined reference to `strlen'
main.o: In function `main':
main.c:(.text+0x9b): undefined reference to `init_video'
Edit
http://pastebin.com/ibBAVpe5 - Main.c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还没有提供
strlen
,那么它到底应该来自哪里?在正常编译中,ld
命令行将由cc
添加一个-lc
以引入标准库。鉴于有迹象表明这是独立的,您将需要提供自己的支持例程,例如strlen
和memcpy
;你不能使用标准 C 库(或者必须非常小心),因为它会尝试调用内核来执行某些操作。#
这并不能解释重新排序对象的其他问题,也不能解释下面评论中的讨论。这让我相信您有一个损坏的或可能不兼容的链接描述文件(
-T link.ld
)。粘贴那个?You haven't provided a
strlen
, so where exactly should it come from? In a normal compile, theld
command line will have an-lc
added to it bycc
to bring in the standard library. Given the indications that this is intended to be standalone, you will need to provide your own support routines such asstrlen
andmemcpy
; you can't use a standard C library (or would have to be very careful about it) because that will try to call into the kernel for some things.#
That doesn't explain the other problem with reordering objects, nor the discussion in the comments below. This leads me to believe you have a broken or possibly incompatible linker script (the
-T link.ld
). Paste that?