gpt4 book ai didi

c - Code::Blocks 中的头文件和源文件问题

转载 作者:行者123 更新时间:2023-11-30 15:13:04 24 4
gpt4 key购买 nike

我正在使用 Code::Blocks 用 C 语言编写游戏。我正在使用最新版本的 C 和 Code::Blocks。我仍在学习这门语言。

我过去的所有程序都包含在一个巨大的源文件中,因此我决定扩展并尝试将我的代码放入多个文件中。我知道正确的方法是拥有包含代码定义等的源文件以及包含其他源文件使用的原型(prototype)的头文件。这对我来说非常糟糕,我要么无法让文件一起正常工作,要么根本不起作用。

我在名为 process.c 的源文件中有一个函数,在名为 process.h 的文件中有一个函数原型(prototype)。我还有一个 main.h 和一个 main.c 包含所有其余代码。主要问题是我的 main.h 文件中有一个 typedef struct Game 并且我无法获取我创建的用于在我的 process.c。我的游戏中的每个功能都需要 Game 类型才能工作。但是,当我授予 process.c 访问 main.h(声明 Game 的文件)的权限时,我遇到了问题。

我的代码在一个文件中时工作正常。我的头文件受到保护,不会重复,并正确包含在程序中。问题是,我需要在 main.cprocess.c 中包含 main.h 。我必须在“main.c”和“process.c”中包含process.h。我已经尝试了所有配置,但没有任何效果。

在某些 #include 配置中,我没有收到任何错误,但我收到一条奇怪的消息,上面写着“您的项目似乎尚未构建;您想现在构建它吗?”当我单击"is"时,什么也没有发生。

我的编译器工作正常,项目设置没有任何问题。这到底是怎么回事?如何让 main.hprocess.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

进程.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);
}

进程.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 中,您有错误的 header block :

#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);

此外,同一文件中的函数协议(protocol)不正确。您正在尝试实例化不存在的变量类型:(请参阅 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 该文件的任何文件中创建该结构的实例。

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)

4) 在 main.c 中,函数 initGame 中有需要以不同方式引用的变量。更改此:

void initGame(Game *thisGame)
{
variable = 10;
number = 5;
letter = 'c';
}

致:

void initGame(GAME *thisGame)
{
thisGame->variable = 10;
thisGame->number = 5;
thisGame->letter = 'c';
}

Code::Blocks information here 这可能会帮助您设置环境以获取错误消息,从而帮助您调试代码。

关于c - Code::Blocks 中的头文件和源文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34842655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com