【问题标题】:using header files in multiple .c sources在多个 .c 源中使用头文件
【发布时间】:2018-12-22 15:37:39
【问题描述】:

在我的 CLion 项目中,我有一些标题。其中之一是constants.h,我将所有常量都放入其中。现在,我想在main.cview.h 中使用这个标头。 view.c 是另一个与view.h 关联的源文件。每当我使用它时,它都会因为重新定义常量而出错。我也用过:

//constants.h
#ifndef PROJECT_CONSTANTS_H
#define PROJECT_CONSTANTS_H
# define pi 3.14159265359
# define degToRad (2.000*pi)/180.000

//GAME GRAPHICS CONSTANTS
const int TANK_RADIUS = 15;
const int CANNON_LENGTH = TANK_RADIUS;
const int BULLET_RADIUS = 4;
const int BULLET_SPAWN_POS = TANK_RADIUS+BULLET_RADIUS;
//const int tank_width = 10;
//const int tank_height = 20;
const int WIDTH = 800;
const int HEIGHT = 600;

//GAME LOGICAL CONSTANTS
const int step = 5;
const double angleStep = 4.5;
const int bulletSpeed = 8;
#define maxBulletPerTank  10
#endif

//main.c includes
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <SDL.h>
#include <SDL2_gfxPrimitives.h>
#include "structs.h"


#include "view.h"

//view.h
#ifndef PROJECT_VIEW_H
#define PROJECT_VIEW_H

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <SDL.h>
#include <SDL2_gfxPrimitives.h>
#include "constants.h"


SDL_Renderer* init_windows(int width , int height);

#endif

//view.c

#include "view.h"
SDL_Renderer* init_windows(int width , int height)
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("workshop", 100, 120, width, height, SDL_WINDOW_OPENGL);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    return renderer;
}

在 constants.h 的第一部分,但在 main.cview.h 中都包含它会给我错误。有没有办法解决这个问题?请注意,如果我没有将它包含在 view.h 中,它就无法识别某些使用constants.h 中定义的常量的部分。我需要在其他几个.h 文件中使用这个常量。

main.cview.h 的顶部我有:#include&lt;constants.h&gt; 并且我在view.c 的顶部有#include&lt;view.h&gt;view.h也包含在main.c的顶部

其中一个错误:

CMakeFiles\project_name.dir/objects.a(view.c.obj):[address]/constants.h:26: multiple definition of `step':
CMakeFiles\project_name.dir/objects.a(main.c.obj):[address]/constants.h:23: first defined here

我是标准编程的新手,不知道如何处理。

【问题讨论】:

  • 您能告诉我们您的错误信息吗?
  • @NaWeeD 我添加了 constants.h 代码和其中一个错误。
  • constants.h 本身看起来还不错,并且具有正确的 #ifndef/#define 样板可以工作。 view.h 中的 #ifndef/#define 设置是否也相同?文件如何相互导入?
  • step 是否在 view.c 的其他地方声明?如果问题出在整个包含中,则错误应该是关于TANK_RADIUS,这是其中的第一个定义,而不是step。它只对step 失败的事实引发了问题。

标签: c header-files


【解决方案1】:

问题在于您的标题没有定义常量,它定义了(只读)变量。你会得到一个错误,因为#include 将标头的内容复制到包含它的每个源文件中,因此每个源文件都会尝试定义这些全局变量。

当你想要全局变量时,通常的解决方案是只将声明放在头文件中:

// constants.h
extern const int step;
extern const double angleStep;

实际的变量定义在(一个)源文件中:

// constants.c
#include "constants.h"
const int step = 5;
const double angleStep = 4.5;

但请注意,这些不是常量:

switch (...) {
    case step:  // Error: case label must be a constant
        ...
}

如果你想要实际的常量,你应该使用宏来代替:

// constants.h
#define STEP 5
#define ANGLE_STEP 4.5

那么您也不需要单独的.c 文件。

仅对于整数常量,有一个涉及枚举的解决方法:

// constants.h
enum { step = 5 };

这将创建一个名为step 的实际整数常量;不幸的是,这种技术不能用于浮点数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多