2011-11-24 212 views
0

我试着去编译汇编语言和C代码放在一起(不是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)' 

任何方式拿C定义到汇编usi中ng gcc?

在此先感谢。

回答

1

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

# gcc -c common.S 

没有

#include <common.h> 

在common.S

+0

林不知道这是一个选项。将。#include包含在.S中的想法不是我的。我在'arch /'部分读取OS代码,并在那里包含.h文件。 – Tom

+0

Ahh OK ..离开include并将其编译为不带头文件,然后 - 如果包含头文件,则不需要编译头文件。 –

+0

'#GCC -c common.S'相同的输出:( – Tom

2

不,你不能包括在汇编语言C声明。汇编程序不知道什么是struct tree

如果您想编写一个汇编语言函数foo,它使用了您的定义struct tree,您将不必使用C头文件。

要得到什么,这可能是什么样子,写在C foo功能,具有gcc -S编译生成汇编清单的想法,然后再看看所产生的编译器生成的common.s文件。 (你或许应该这样做在一个单独的目录,这样你就不会破坏现有的common.s文件)。

你可能不会看到struct tree或成员名称leftrightelem任何引用;相反,您会看到以某些偏移量引用数据的汇编操作码。

-1

我建议您查看Inline Assembler Cookbook

+1

大家都知道,你链接到一个文件:///本地到你的机器,右 –

+0

@jonathon莱因哈特哎呀,http://www.nongnu.org /avr-libc/user-manual/inline_asm.html – Jeff