在我的代码中,我得到了大量这样的错误。不知道为什么。以下是错误的示例:
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
下面是我的rtype.c的第4-6行
void rToMachine(char* line, char* mach, int currentSpot, instr currentInstruction,
rcode* rcodes)
{ 对于rtype.c中声明的每个函数,我都会收到类似这样的错误
有什么想法吗?谢谢你们!
发布于 2013-04-26 12:58:46
因为在评论中正确地写出来太长了,所以我添加了这个作为答案。
在处理多个源文件时,您应该将它们逐个编译成目标文件,然后在单独的步骤中将它们链接在一起,形成最终的可执行程序。
首先制作目标文件:
$ 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"https://stackoverflow.com/questions/16228958
复制相似问题