2013-04-26 99 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" 
相关问题