我正在学习来自 here 的内核教程
我在编译我的文件时遇到问题。
当我尝试编译时出现以下错误:
main.c:8: error: expected declaration specifiers or ‘...’ before ‘size_t’
main.c:8: error: conflicting types for ‘memcpy’
./include/system.h:5: note: previous declaration of ‘memcpy’ was here
main.c: In function ‘memcpy’:
main.c:12: error: ‘count’ undeclared (first use in this function)
main.c:12: error: (Each undeclared identifier is reported only once
main.c:12: error: for each function it appears in.)
main.c: At top level:
main.c:16: error: expected declaration specifiers or ‘...’ before ‘size_t’
main.c:16: error: conflicting types for ‘memset’
./include/system.h:6: note: previous declaration of ‘memset’ was here
main.c: In function ‘memset’:
main.c:19: error: ‘count’ undeclared (first use in this function)
main.c: At top level:
main.c:23: error: expected declaration specifiers or ‘...’ before ‘size_t’
main.c:23: error: conflicting types for ‘memsetw’
./include/system.h:7: note: previous declaration of ‘memsetw’ was here
main.c: In function ‘memsetw’:
main.c:26: error: ‘count’ undeclared (first use in this function)
main.c: At top level:
main.c:30: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strlen’
main.c:49: warning: return type of ‘main’ is not ‘int’
main.c: In function ‘main’:
main.c:64: warning: pointer targets in passing argument 1 of ‘puts’ differ in signedness
./include/system.h:13: note: expected ‘unsigned char *’ but argument is of type ‘char *’
main.c:51: warning: unused variable ‘i’
scrn.c: In function ‘scroll’:
scrn.c:24: warning: passing argument 1 of ‘memcpy’ from incompatible pointer type
./include/system.h:5: note: expected ‘unsigned char *’ but argument is of type ‘short unsigned int *’
scrn.c:24: warning: passing argument 2 of ‘memcpy’ from incompatible pointer type
./include/system.h:5: note: expected ‘const unsigned char *’ but argument is of type ‘short unsigned int *’
scrn.c: In function ‘puts’:
scrn.c:139: warning: pointer targets in passing argument 1 of ‘strlen’ differ in signedness
./include/system.h:8: note: expected ‘const char *’ but argument is of type ‘unsigned char *’
我的文件是教程中文件的精确副本。
我可以看到在 main.c 中函数是这样定义的
void *memcpy(void *dest,const void *src, size_t count)
但在我的 system.h 文件中,它们是这样定义的
extern unsigned char *memcpy(unsigned char *dest,const unsigned char *src, int count)
C 不是我的主要语言,但我正在学习它,所以如果我的问题很简单,我深表歉意,但我认为这些定义应该相同,不是吗?
最佳答案
您的问题可能是 size_t
与您平台上的 int
不同,或者根本没有正确指定 size_t
.指针类型应该没问题(从技术上讲,它们也应该匹配,但在大多数系统上 sizeof(char*) == sizeof(void*)
)。
如果您正在开发自己的内核,您会想要编写自己的system.h
。如果您正在编写 system.h
和 main.c
,您可以根据需要使它们匹配。如果你看this page of the tutorial ,您会看到 header 和 C 源代码都将 memcpy
声明为:
unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count);
但是如果您在本教程末尾下载示例源文件,您会发现它是:
void *memcpy(void *dest, const void *src, size_t count);
查看该文件的顶部,您会发现以下注释:
/* bkerndev - Bran's Kernel Development Tutorial
* By: Brandon F. (friesenb@gmail.com)
* Desc: Main.c: C code entry.
*
* Notes: No warranty expressed or implied. Use at own risk. */
看起来您并不是在尝试按照教程进行操作,而是在尝试从教程中剪切和粘贴代码。这就像跟着书本学习做脑部手术一样。你可能会让它工作,但如果你不真正理解你在做什么......好吧,请帮这个世界一个忙,不要将它用于任何重要的事情。
关于编译我自己的内核(不是来自 linux-kernel 源代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1688844/