【问题标题】:Trouble compiling C code: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'int'编译 C 代码时遇到问题:错误:在 'int' 之前需要 '='、','、';'、'asm' 或 '__attribute__'
【发布时间】:2017-05-20 20:14:06
【问题描述】:

我在编写内核时使用James M tutorials。我在 macOS 10.12 上使用带有 GCC 6.2.0 和 Binutils 的 elf-i386 架构的交叉编译器编写代码。

一切都编译除了 main.c,失败并出现此错误:

错误:之前应为 '='、','、';'、'asm' 或 '__attribute__' 'int'.

但是,该文件与教程中的完全相同。谁能帮我弄清楚为什么?

/*
Sierra Kernel
kernel.c -- Main kernel file
Copyright (C) 2017  Jacobo Soffer <sofferjacob@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>. 
*/

#include <os.h>

int main(struct multiboot *mboot_ptr)
{
    kprint("Welcome to the Sierra Kernel! \n"); // Test VGA Driver
    kprint(BUILD_FULL); // Print version information
    asm volatile ("int $0x3");  // Test interrupts
    asm volatile ("int $0x4");
}

包含所有内核代码的公共存储库位于:https://gitlab.com/SierraKernel/Core/tree/master

【问题讨论】:

  • 你好像多了一个逗号:int main(struct multiboot *mboot_ptr)
  • 请注意,您需要仔细考虑 main() 的高度非标准的 struct multiboot * 参数将来自何处。您必须在嵌入式系统上工作,但是如果您正在编写内核,那么将如何初始化并将该结构传递给内核?

标签: c gcc


【解决方案1】:

可能是一个迟到的答案,但通常错误指向 int 之前包含的文件中不正确的内容,在您的情况下它是 os.h ,尝试发现此文件中是否缺少 ;或它包含的任何文件。

【讨论】:

    【解决方案2】:
    int main(struct multiboot, *mboot_ptr)
    

    好像多了一个","

    试试

    int main(struct multiboot *mboot_ptr)
    

    【讨论】:

    • 我删除了逗号,但是当我尝试编译时仍然得到同样的错误。感谢您的帮助
    【解决方案3】:

    如果您尝试使用 c89 C 方言进行编译,您会遇到这个问题。尝试c99 CFLAGS+ = -std=c99

    【讨论】:

    • 我的意思是尝试使用c99方言。
    • CFLAGS+ = -std=c99
    【解决方案4】:

    对于我来说,这个错误是由于一个错误的宏定义在名称和第一个括号之间有一个空格:

           #define DEFINE_EQUAL_NUM (type) \
            maccro_body...
    

    应该是

    #define DEFINE_EQUAL_NUM(type) \
                maccro_body...
    

    希望这个答案对某人有所帮助,我在一个复杂的基于宏的模块中度过了很多时间,当我看到它时真的很伤心:(

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多