In my project in CLion, I have some headers. One of them is constants.h
in which I put all my constants. Now, I want to use this header in main.c
and view.h
. view.c
is another source file which is associated with view.h
. Whenever I use it, it gives error because of redefinition of constants. I also used:
//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;
}
At the first part of constants.h but including it in both main.c
and view.h
gives me the error. Is there anyway to solve this? Note that if I don't include it in view.h, it don't recognize some parts that use constants defined in constants.h
. I need to use this constants in several other .h
files.
at top of main.c
and view.h
I have: #include<constants.h>
and I have #include<view.h>
at top of view.c
. view.h
is also included at top of main.c
One of the Errors:
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
I am new to Standard Programming and don't know how to handle this.