- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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.c
和 process.c
中包含 main.h
。我必须在“main.c”和“process.c”中包含process.h
。我已经尝试了所有配置,但没有任何效果。
在某些 #include
配置中,我没有收到任何错误,但我收到一条奇怪的消息,上面写着“您的项目似乎尚未构建;您想现在构建它吗?”当我单击"is"时,什么也没有发生。
我的编译器工作正常,项目设置没有任何问题。这到底是怎么回事?如何让 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
进程.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/
我有一个项目,其中包含两个源文件和一个头文件(其中包含名为 Get.c 的源文件之一中定义的函数的原型(prototype)),问题是: 当我在另一个源文件中包含 Get.c 时,它给出了错误 错误:
有没有尽可能多的语法结构的java源代码文件? java 编译器的某种测试文件。我需要该文件来测试 java 源代码解析器。 最佳答案 看看 Java Compatibility Kit ,特别Tec
尝试在命令提示符下编译时,我在使用 codeblocks-16.01mingw-setup.exe - 安装在路径不包含空格的文件中时遇到问题。初学者指南建议在命令提示符中使用以下行: gcc car
我编写了一个使用 Cryptopp 库的程序。我已经创建了静态库并将其包含在我的分发版中,但是该程序还需要很多头文件。我的问题是我需要在我的发行版中包含所有这些头文件吗? 例如,我编写了一个代码,其中
我在源代码树中有 protobuf 原型(prototype)文件。我想在每次更改和首次运行时从原型(prototype)文件生成源文件(例如,我创建新的原型(prototype)文件)。然后,我想从
我知道在 Java 中加载文件而不指定要使用的编码是平台相关的。但我的问题是关于 .java 源文件本身中包含的文本:用于这些文件的编码是否仍然相关一旦编译? 例如,如果我在 Windows 上有一个
编辑:{ 我想我在这里添加了很多(太多)信息(阅读)。我想要的是: 我正在使用一个网站(没有.csproj文件) 我需要多个源代码文件才能运行我的aspx 据我所知,当请求aspx文件时,我要么需要使
我从 Asio 的示例页面中获取了以下代码 class tcp_connection : public boost::enable_shared_from_this { public: t
我已经使用 VS2010 为我的 C++ 应用程序创建了一个 Windows 安装部署。但是我的问题是我不知道如何将我自己的一些代码压缩到安装向导中(这可能吗?)。问题是在安装过程中我想要: 要求用户
最近我创建了一个 bash 脚本,我应该在 cron 中运行它。 准备好bash脚本并正常运行后,我将其放入Cron中,发现它失败了。作为第二步,我删除了所有环境依赖项,即我指定的不是 file.tx
考虑以下场景 例子.txt: ÄäÖöÜü Java 源代码: try (FileInputStream fileInputStream = new FileInputStream("example.
背景我主要使用嵌入式 C/C++ 进行编程,但有时我必须为我们的 API 进行一些 C# 编程。为此,我使用 Visual Studio 2017 为我们的客户创建 API DLL。 C# API 和
这个问题在这里已经有了答案: ggplot's qplot does not execute on sourcing (1 个回答) 关闭 9 年前。 考虑这种形式的源文件: # initializ
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我需要读取一个 Racket 源文件并通过宏扩展来运行它。我有一个简单的测试文件,Racket 本身很乐意接受: C:\ayane>type factorial.rkt #lang racket (p
我的源文件位于此文件夹中:c:\data\mycompany。我的所有源文件的第一行都包含以下内容:package mycompany;现在,我从 c:\data 文件夹中使用以下命令编译了所有内容:
这个问题在这里已经有了答案: #include all .cpp files into a single compilation unit? (6 个回答) The benefits / disadv
我对这个 JAVA RMI 项目真的很陌生。我只是想知道我可以使用什么编译器来编译我的JAVA源代码?以及如何做到这一点? 我正在尝试编译来自 http://www.eg.bucknell.edu/~
我想使用 Subversion 并仅 check out 源文件(例如:仅 check out .c 、 .cpp 和 .h 文件)。这可能吗 ?如果是这样,我该怎么做? 我正在尝试从以下位置获取 w
有没有办法使用指向唯一头文件的不同配置(或不同方法)来设置单个项目。 我想创建一个包含两个头文件的 C 项目(例如 header1.h 和 header2.h)。在我的 .c 源文件中,我定义了两个配
我是一名优秀的程序员,十分优秀!