2011-11-24 92 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