0

already defined in main.obj error C

Hi I have a simple SDL application and I'm getting error that all variables in main.h are already defined. I don't see where I'm defining them twice. main.h : #pragma once #include <stdio.h> #include <SDL.h> #include <SDL_image.h> const char title[] = "game"; int windowWidth = 800; int windowHeight = 600; int isRunning = 0; SDL_Window* window = NULL; Uint32 render_flags = NULL; SDL_Renderer* renderer = NULL; main.c: #include "textures.h" int main() { if (SDL_Init(SDL_INIT_EVERYTHING) == 0) { printf("SDL initialized successfully\n"); } else { printf("SDL INIT ERROR"); SDL_Quit(); return -1; } window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight, 0); if (window) { printf("SDL Window created successfully\n"); } else { printf("ERROR creating window"); SDL_DestroyWindow(window); SDL_Quit(); return -1; } render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC; renderer = SDL_CreateRenderer(window, -1, render_flags); if (renderer != 0) { printf("SDL Renderer created successfully\n"); } else { printf("ERROR creating renderer"); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; } isRunning = 1; // MAIN LOOP SDL_RenderClear(renderer); SDL_RenderCopy(renderer, tex, NULL, NULL); SDL_RenderPresent(renderer); SDL_Delay(5000); // clean up resources before exiting SDL_DestroyTexture(tex); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); return 0; } textures.h: #include "main.h" SDL_Surface* surface = NULL; SDL_Texture* tex = NULL; void LoadTextures(); textures.c: #include "textures.h" void LoadTextures() { surface = IMG_Load("test.png"); tex = SDL_CreateTextureFromSurface(renderer, surface); SDL_FreeSurface(surface); }

19th Nov 2020, 10:15 PM
Kry$tof
4 Respuestas
+ 1
In your .c file it should be int windowWidth = 800; That's with the int keyword before. As with the new error, did you happen to declare renderer as an int variable by accident?
21st Nov 2020, 9:46 AM
The Man
The Man - avatar
0
When you write int windowWidth = 800; This defines a new variable. You probably include this header in multiple .c files and this defines the variable multiple times. Instead you should define the variable in a .c file and write extern int windowWidth; in the header file.
20th Nov 2020, 9:46 AM
The Man
The Man - avatar
0
The Man I added extern before every variable in main.h for example I have only extern int windowWidth; and main.c I have only windowWidth = 800; now I'm getting error renderer int differs in levels of indirection from SDL_Renderer
20th Nov 2020, 12:45 PM
Kry$tof
0
The Man Thanks I finally got it working with extern word.
21st Nov 2020, 11:24 AM
Kry$tof