【问题标题】:Compile Assembly code that includes header files containing C-definitions编译包含包含 C 定义的头文件的汇编代码
【发布时间】:2011-11-24 02:07:19
【问题描述】:

我正在尝试将程序集和 C 代码一起编译(不是 C 到程序集),但无法完成。

例如

文件 common.h

#ifndef __COMMON_H__
#define __COMMON_H__

struct tree{
        tree* left;
        tree* right;

        void* elem;
};

void foo(int c);
#endif

文件common.S

#include "common.h"

    .text
    .globl foo
    .ent foo
foo:
     //foo implementation

    .end foo

当我尝试编译时:

# gcc -c common.S
common.h: Assembler messages:
common.h:5: Error: unrecognized opcode `struct tree{'
common.h:7: Error: unrecognized opcode `tree* left'
common.h:8: Error: unrecognized opcode `tree* right'
common.h:10: Error: unrecognized opcode `void* elem'
common.h:12: Error: junk at end of line, first unrecognized character is `}'
common.h:14: Error: unrecognized opcode `void foo(int c)'

有什么方法可以使用 gcc 将 C 定义带入汇编?

提前致谢。

【问题讨论】:

    标签: c gcc compiler-construction assembly avr-gcc


    【解决方案1】:

    不,您不能在汇编语言中包含 C 声明。汇编器不知道struct tree 是什么意思。

    如果你想编写一个汇编语言函数foo 来使用你对struct tree 的定义,你将不得不在不使用C 头文件的情况下这样做。

    要了解这可能是什么样子,请用 C 语言编写 foo 函数,用 gcc -S 编译它以生成汇编列表,然后查看编译器生成的 common.s 文件. (您可能应该在单独的目录中执行此操作,以免破坏现有的 common.s 文件。)

    您可能不会看到对struct tree 或成员名称leftrightelem 的任何引用;相反,您会看到在特定偏移处引用数据的汇编操作码。

    【讨论】:

      【解决方案2】:

      你不需要编译头文件。试试这个:

      # gcc -c common.S
      

      没有

      #include <common.h> 
      

      共同点。S

      【讨论】:

      • 我不确定这是一个选项。在 .S 中包含#include 的想法不是我的。我正在阅读arch/ 部分中的操作系统代码,其中包含 .h 文件。
      • 啊,好吧..离开包含并在没有头文件的情况下编译它——如果包含头文件,则不需要编译头文件。
      • # gcc -c common.S 相同的输出 :(
      【解决方案3】:

      我建议您查看Inline Assembler Cookbook

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      相关资源
      最近更新 更多