我正在使用 Code::Blocks 用 C 语言编写游戏。我正在使用最新版本的 C 和 Code::Blocks。我还在学习语言。
我过去的所有程序都包含在一个巨大的源文件中,因此我决定进行分支并尝试将我的代码放入多个文件中。我知道正确的方法是拥有包含代码定义等的源文件以及包含其他源文件使用的原型的头文件。这对我来说非常糟糕,我要么无法让文件正常工作,要么根本不起作用。
我在名为
process.c
的源文件中有一个函数,在名为 process.h
的文件中有一个函数原型。我还有一个 main.h
和 main.c
包含所有其余代码。主要问题是我的 typedef struct Game
文件中有一个 main.h
,但我无法获取我创建的用于在我的 process.c
中工作的“游戏”结构类型。我的游戏中的每个功能都需要 Game
类型才能工作。但是,当我授予 process.c
访问 main.h
(声明 Game
的文件)时,我遇到了问题。
我的代码在一个文件中时工作正常。我的头文件受到保护,不会重复,并正确包含在程序中。问题是,我需要在
main.h
和 main.c
中包含 process.c
。我必须在“main.c”和“process.c”中都包含process.h
。我已经尝试了所有配置,但没有任何效果。
在某些
#include
配置中,我没有收到任何错误,但我收到一条奇怪的消息,上面写着“您的项目似乎尚未构建;您想现在构建它吗?”当我单击“是”时,什么也没有发生。
我的编译器工作正常,项目设置没有任何问题。这到底是怎么回事?如何让
main.h
和 process.h
协同工作?
编辑:源代码:
main.c:
#include <stdio.h>
#include "main.h"
#include "process.h"
void initGame(Game *thisGame)
{
variable = 10;
number = 5;
letter = 'c';
}
int main()
{
Game thisGame;
initGame(&thisGame);
displayData(&thisGame);
return 0;
}
main.h:
#ifndef _MAIN_H_
#define _MAIN_H_
typedef struct
{
int variable, number;
char letter;
}
#endif
process.c:
#include <stdio.h> //not sure if this should be here or not, it doesn't seem to effect my code
#include "main.h"
#include "process.h"
void displayData(Game *thisGame)
{
printf("%i, %i, %c", thisGame.variable, thisGame.number, thisGame.letter);
}
process.h:
#ifndef _MAIN_H_
#define _MAIN_H_
void displayData(Game *thisGame);
#endif
-------------- Build: Debug in FishKiller (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -L..\deps\lib -L..\SDLFILES\lib -o bin\Debug\FishKiller.exe obj\Debug\main.o obj\Debug\process.o -lmingw32 -lSDL2main -lSDL2 -lSDL2_image
obj\Debug\process.o:process.c:(.rdata+0x0): multiple definition of `SCREEN_WIDTH'
obj\Debug\main.o:main.c:(.rdata+0x0): first defined here
obj\Debug\process.o:process.c:(.rdata+0x4): multiple definition of `SCREEN_HEIGHT'
obj\Debug\main.o:main.c:(.rdata+0x4): first defined here
obj\Debug\process.o:process.c:(.rdata+0x8): multiple definition of `GAMESTATE_MENU'
obj\Debug\main.o:main.c:(.rdata+0x8): first defined here
obj\Debug\process.o:process.c:(.rdata+0xc): multiple definition of `GAMESTATE_GAME'
obj\Debug\main.o:main.c:(.rdata+0xc): first defined here
obj\Debug\process.o:process.c:(.rdata+0x10): multiple definition of `GAMESTATE_GAMEOVER'
obj\Debug\main.o:main.c:(.rdata+0x10): first defined here
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
下面逐个文件解决了问题。一旦这些问题在源代码中得到纠正,可执行文件就会构建。
1) 在 process.h 中,您有错误的标头块:
#ifndef _MAIN_H_
#define _MAIN_H_
//Change to:
#ifndef _PROCESS_H_
#define _PROCESS_H_
同时更改:
void displayData(Game *thisGame);(see notes in main.h for explanation)
致:
void displayData(GAME *thisGame);
2)在process.c中你有;
#include "main.h"
它不会造成任何伤害,但由于我们正在分析所有内容,因此不需要它来支持当前的架构。
您还拥有:
printf("%i, %i, %c", thisGame.variable, thisGame.number, thisGame.letter);
因为
thisGame
是作为指针传入的,所以需要使用结构体指针运算符:->
printf("%i, %i, %c", thisGame->variable, thisGame->number, thisGame->letter);
此外,同一文件中的函数协议不正确。您正在尝试实例化一个不存在的变量类型:(请参阅 main.h 的注释)
变化:
void displayData(Game *thisGame){...}
致:
void displayData(GAME *thisGame){...}//uses typedef struct GAME
3)在main.h中你有一个格式错误的结构:
typedef struct
{
int variable, number;
char letter;
}//no ";" statement terminator to indicate to your compiler _end of struct_
通过这个定义,没有结构体name可以在任何其他文件中引用或实例化它。将其更改为以下内容:
typedef struct
{
int variable;
int number;//style point , put each member on its own line
char letter;
}GAME;//note struct type name and terminator ";"
使用结构类型名称(在本例中为 GAME),您可以在任何包含此文件的文件中创建此结构的实例。
#includes
4) 在 main.c 中,函数 extern GAME Game;// using the extern modifier, create an instance of GAME
// that can be referenced in any file of the
//project, once initialized. (See the line GAME Game; in main.c)
中有需要以不同方式引用的变量。改变这个:
initGame
致:
void initGame(Game *thisGame)
{
variable = 10;
number = 5;
letter = 'c';
}
这里有
Code::Blocks 信息,可以帮助您设置环境以获取错误消息,从而帮助您调试代码。