2013-04-26 106 views
1

我在代碼中收到了大量類似這樣的錯誤。無法弄清楚原因。下面是錯誤的一個例子:預期'=',',',';','asm'或'__attribute__'在'{'標記之前

In file included from mipstomachine.c:2:0, 
       from assembler.c:4: 
rtype.c: In function ‘getRegister’: 
rtype.c:6:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token 

我的當前文件的佈局,作爲闡述,具有mipstomachine.c,其包括assembler.c,其包括rtype.c

這裏是線4-6我rtype.c

void rToMachine(char* line, char* mach, int currentSpot, instr currentInstruction, 
          rcode* rcodes) 
{ 

我得到這樣的錯誤在rtype.c

任何想法宣稱每一個功能?多謝你們!

+0

消息說'rtype.c:In function'getRegister':',但是你沒有顯示相關的代碼。你能提供更多的背景嗎? – 2013-04-26 04:48:58

+2

你缺少'}'一些在我猜測之前的那個函數。 – Jeyaram 2013-04-26 04:49:03

+3

你有源文件,其中包含其他源文件?這不是真的推薦,我實際上會說它是邊界錯誤的。將函數原型和結構放在頭文件中,並在需要時將它們包含在源文件中,然後將源文件鏈接在一起。 – 2013-04-26 04:51:19

回答

4

由於在評論中寫得很好,所以我將其添加爲答案。

當處理多個源文件時,應該將它們逐個編譯成目標文件,然後在單獨的步驟中將它們鏈接在一起以形成最終的可執行程序。

首先使對象文件:

$ gcc -Wall -g file_1.c -c -o file_1.o 
$ gcc -Wall -g file_2.c -c -o file_2.o 
$ gcc -Wall -g file_3.c -c -o file_3.o 

標誌-c告訴GCC生成目標文件。標誌-o告訴GCC什麼來命名輸出文件,在這種情況下是對象文件。額外的標誌-Wall-g告訴GCC生成更多的警告(總是很好,修復警告可能實際上修復了可能導致運行時錯誤的事情)並生成調試信息。

然後將這些文件鏈接在一起:

$ gcc file_1.o file_2.o file_3.o -o my_program 

這個命令告訴GCC調用鏈接程序和所有指定目標文件鏈接成可執行程序my_program


如果在多個源文件中存在需要的結構和/或函數,那就是當您使用頭文件時。

例如,假設你有一個結構my_structure並需要從多個源文件中的函數my_function,你可以這樣創建一個頭文件header_1.h

/* Include guard, to protect the file from being included multiple times 
* in the same source file 
*/ 
#ifndef HEADER_1 
#define HEADER_1 

/* Define a structure */ 
struct my_structure 
{ 
    int some_int; 
    char some_string[32]; 
}; 

/* Declare a function prototype */ 
void my_function(struct my_structure *); 

#endif 

這個文件現在可以包含在這樣的源文件中:

#include "header_1.h" 
相關問題