I am programming a game in C using Code::Blocks. I am using the most recent version of C and of Code::Blocks. I am still learning the language.
All of my past programs have been contained in one huge source file, so I decided to branch out and try putting my code in multiple files. I know that the proper way to do it is to have source files containing code definitions and such and a header file containing prototypes for other source files to use. This has worked out horribly for me and I either can't get the files to work together properly or it simply doesn't work period.
I have a single function in a source file called process.c and a function prototype in a file called process.h. I also have a main.h and a main.c containing all the rest of the code. The main issue is that I have a typedef struct Game in my main.h file and I can't get the 'Game' struct type I created to work in my process.c. Every function in my game needs the Game type in order to work. However, when I give process.c access to main.h (the file that Game is declared in) I get issues.
My code works fine when it's in one file. My header files are protected from duplication and are properly included in the program. The problem is, I need to include main.h in both main.c and process.c. And I have to include process.h in both 'main.c' and 'process.c'. I have tried every configuration and nothing works.
In some #include configurations I get no errors, but I get this weird message that says "It seems your project has not been built yet; would you like to build it now?" and when I click "Yes" nothing happens.
My compiler works fine and there is nothing wrong with the projects settings. What the heck is going on here? How do I get main.h and process.h to work together?
EDIT: Source code:
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;}#endifprocess.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);}process.h:
#ifndef _MAIN_H_ #define _MAIN_H_void displayData(Game *thisGame);#endifError message
-------------- 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 hereobj\Debug\process.o:process.c:(.rdata+0x4): multiple definition of `SCREEN_HEIGHT'obj\Debug\main.o:main.c:(.rdata+0x4): first defined hereobj\Debug\process.o:process.c:(.rdata+0x8): multiple definition of `GAMESTATE_MENU'obj\Debug\main.o:main.c:(.rdata+0x8): first defined hereobj\Debug\process.o:process.c:(.rdata+0xc): multiple definition of `GAMESTATE_GAME'obj\Debug\main.o:main.c:(.rdata+0xc): first defined hereobj\Debug\process.o:process.c:(.rdata+0x10): multiple definition of `GAMESTATE_GAMEOVER'obj\Debug\main.o:main.c:(.rdata+0x10): first defined herecollect2.exe: error: ld returned 1 exit statusProcess terminated with status 1 (0 minute(s), 0 second(s))0 error(s), 0 warning(s) (0 minute(s), 0 second(s))