【问题标题】:kernel.cpp showing error and Makefile error while making kernel.o [closed]kernel.cpp 在制作 kernel.o 时显示错误和 Makefile 错误 [关闭]
【发布时间】:2018-01-05 15:16:23
【问题描述】:

错误是:(我理解警告,请纠正错误)

root@kali:~/Desktop/Ohm Os# make kernel.o

g++ -m32 -o kernel.o -c kernel.cpp

kernel.cpp: In function ‘void kernelMain(void*, unsigned int)’:

kernel.cpp:27:25: warning: ISO C++ forbids converting a string constant to 

‘char*’ [-Wwrite-strings]

     printf("HELLO WORLD");
                         ^
kernel.cpp:31:1: error: expected primary-expression before ‘}’ token
 }
 ^

Makefile:9: recipe for target 'kernel.o' failed

make: *** [kernel.o] Error 1

这是我的 KERNEL.CPP:

void printf(char* str)
{
    unsigned short* VideoMemory = (unsigned short*)0xb8000;
    for(int i = 0; str[i] != '\0'; ++i)
        VideoMemory[i] = (VideoMemory[i] & 0xFF00) | str[i];
}

void kernelMain(void* multiboot_structure, unsigned int magicnumber)
{
    printf("HELLO WORLD");
    while(1)
}

还有我的 Makefile:

GPPPARAMS = -m32
ASPARAMS = --32
LDPARAMS = -melf_i386
ojects = loader.o kernel.o

%.o: %.cpp
    g++ $(GPPPARAMS) -o $@ -c $<

%.o: %.s
    as $(ASPARAMS) -o $@ $<

mykernel.bin: linker.ld $(objects)
    ld $(LDPARAMS) -T $< -o $@ $(objects)

install: mykernel.bin
    sudo cp $< /boot/mykernel.bin

【问题讨论】:

  • 为什么要用make 变量和后缀规则重新发明轮子?默认情况下有一些非常好的,可以很容易地适应大多数用途。它们还具有被普遍理解的优势,这有助于您的 makefile 的可维护性。
  • 在结尾处缺少一些东西:while(1)。错字?
  • 是的,你可能会在警告中幸存下来。但是为什么printf 期待char*?在抱怨 const char *char * 转换的编译器中看到这种情况很奇怪。您是否尝试将旧标准库与现代编译器一起使用?
  • 我正在开发一个我 18 岁的操作系统,感谢大家的帮助
  • 我不建议使用您不太熟悉的语言编写操作系统内核。也请看OSDEV

标签: c++ linux makefile operating-system kernel


【解决方案1】:

kernel.cpp:31:1: 错误: '}' 标记之前的预期主表达式 }

尝试: 而(1);

【讨论】:

    【解决方案2】:

    您确定您了解警告的后果吗?

    至于错误,您似乎混淆了do whilewhile 的语法

    您需要使用

    while (condition)
      statement
    

    do
      statement
    while (condition)
    

    【讨论】:

    • 其实我认为OP想要一个无限循环来阻止程序退出。
    • 我也这么认为,在这种情况下,条件总是为真。
    猜你喜欢
    • 1970-01-01
    • 2012-08-10
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多