(Help) including header file + other .c file stops code from loading from main.c

Started by
13 comments, last by zaczac 16 years, 8 months ago
Hey there, i've asked this question a few times before on other forums, but no one replies! :( Anyway, it would be great if you guys could help me out. I have a main.c file that includes the header and some functions to call from another .c file called "inventory.c" The rest of the .c file is my main characters movement, jumping, sword slashing etc. Anyway, when compiling my game it will display the graphics that i load in the 'inventory.c" but wont load the character from the "main.c" It seems like it loaded... Main.c - header.h - inventory.c - FINISH It completly misses out the rest of the code located in the .c file to control my character. Is there some function i need to continue loading the rest of the .c file?
Advertisement
I strongly advise that you do not include a .c file in another .c file, and rely on the linker instead. Either way, the exact compiler errors as well as relevant source between [source][/source] tags would be welcome.
Sorry. So your saying that iam to use main.c to call 2 different .c files that can load seperatly and not interupt? I'll try that later.
I didnt use source tags, didnt know i could, plus im using a library built onto C programming and i though that people might start posting that some of my commands were incorrect, and i just wanted an answer to my question. Thank you though!
Quote:Original post by zaczac
Sorry. So your saying that iam to use main.c to call 2 different .c files that can load seperatly and not interupt? I'll try that later.


... interrupt? What do you mean, interrupt? .c files are compiled separately, one translation unit each, they don't interrupt each other.

Quote:I didnt use source tags, didnt know i could, plus im using a library built onto C programming and i though that people might start posting that some of my commands were incorrect, and i just wanted an answer to my question.


Well, if you're using C correctly, nobody's going to have anything to correct. And if there are incorrect things with the way you code, wouldn't improving be a great step forward?

Could someone confirm something for me here?
I have 'main.c' to load the 'header.h' which starts off loading 'inventory.c" and begins to load a background. While my Main.c FAILS to continue loading the rest of my code...

Is that not a correct way to do it? I downloaded an example that had NO header file and just had #include game.c

Im really confused, different examples keep telling me different things.
You should never include other .c-files, only .h ones.
It'd be interesting to know what IDE/compiler you are using, to be able to further help you.
Erm I think you're confusing some of the terminology here. Your "main.c" file doesn't "load" the header which loads the other 'c' file it merely includes it. This gives the functions within the "main.c" file access to the things you have declared within that header. The "inventory.c" file should merely implement (aka; define) the functions that you've declared.

Organizing Code Files in C and C++ is a good place to start to see why and how to do what you're trying to achieve.

The short answer is that, in 'c', you put functions into headers to move them out of your "main.c" file and to help you organise your software. It makes life easier basically. So for your example you would have something like the following:

  • main.c

  • inventory.h - Declaration of the functions and structures you want available in main.c

  • inventory.c - The definition of the functions defined in the header.



Anyway hope that helps but ask here again if you don't understand anything.

Andy

"Ars longa, vita brevis, occasio praeceps, experimentum periculosum, iudicium difficile"

"Life is short, [the] craft long, opportunity fleeting, experiment treacherous, judgement difficult."

Sorry, but heres a brief overview of the code
MAIN.C
// Includes#include // Include for PA_Lib#include "header.h" //I think that does it?//#include "gfx/all_gfx.c"//inc all sprites to .c file//#include "gfx/all_gfx.h" // same but to .h ?// Function: main()int main(int argc, char ** argv){PA_Init();PA_InitVBL();PA_InitText(1,0);//Load C FilesInventoryInit(); // Game Init from game.cLoadInventory(); // Playing the game !!//Varibles declared here//While(1)//Animations, movement


Then in the Header..
#ifndef __HEADER1__#define __HEADER1__#include#include "../source/gfx/all_gfx.h" // Gfx include for the header (.h)void InventoryInit(void); // Game Init...void LoadInventory(void); // Game Play...#endif

And then in the other file "Inventory.c"

//The Inventory Screen, On the Touch Screen//This includes money, falcons, items etc#include <PA9.h>#include "header.h" //I think that does it?// Include the sprites in one and only one .c file...#include "gfx/all_gfx.c"//CHANGE MAYBE TO JUST THE INVENTORY BG?//MainInt();void InventoryInit(void){   //Load FilesPA_EasyBgLoad(0, 3, inventory); //screen, background number, background name	PA_InitText(0, 1);} // End of The Inventory Initvoid LoadInventory(void){		while(1) // Main game loop	{	PA_OutputSimpleText(0,1,1,"My Inventory?"); // Top screen, tile x = 1, y = 1...	PA_WaitForVBL();  	}}



Its made in PALIB, a nintendo ds programming library. Im doing well with all the other coding aspects, movement, AI etc, but i am weak at the knees with using other project files and combining and loading them etc
Sorry for being a jerk and not wanting to post anything, how was i to expect a solution if i was too stubborn to even open up?

I will be grateful for ANY understanding you have!
Right I've tidied up the code a little bit. You had things like "#include" without anything after them, that's just bad syntax and it won't compile if you leave things like that lying around as it's expecting a filename in quotes or angle brackets.

Also I renamed "LoadInventory" to "GameLoop" since it doesn't actually load your inventory but does appear to be your game loop...

Mostly it just appears you had syntax errors. I.e; you'd left off a curly brace or not finished a #include.

Finally it's a good habit to name the header files where you declare a function the same as the file where you will define the function. So "header.h" should really be called "inventory.h" to match "inventory.c"

file : main.c
// Includes//#include // Include for PA_Lib - You can't have empty #include's lying around, thats just abd syntax and won't compile#include "inventory.h" //I think that does it? - yes that includes your header.// Function: main()int main(int argc, char ** argv){	PA_Init();	PA_InitVBL();	PA_InitText(1,0);	//Load C Files	InventoryInit(); // Game Init from game.c	GameLoop(); // Playing the game !!}


file : inventory.h
#ifndef __INVENTORY__#define __INVENTORY__#include "../source/gfx/all_gfx.h" // Gfx include for the header (.h)// function declarations - these just tell whatever includes this file how to use the following functions, you actually "define" them in your "inventory.c"void InventoryInit(void); // Game Init...void GameLoop(void); // Game Play... - "LoadInventory" suggests that you're just loading data from somewhere, whereas GameLoop is a slightly better description.#endif


file : inventory.c
//The Inventory Screen, On the Touch Screen//This includes money, falcons, items etc#include <PA9.h>#include "inventory.h" //I think that does it? - yes this pulls in the declarations for the functions you're about to define.// Include the sprites in one and only one .c file...#include "gfx/all_gfx.c"//CHANGE MAYBE TO JUST THE INVENTORY BG?// function definitions followvoid InventoryInit(void){   //Load Files	PA_EasyBgLoad(0, 3, inventory); //screen, background number, background name	PA_InitText(0, 1);} // End of The Inventory Initvoid GameLoop(void){		while(1) // Main game loop	{		PA_OutputSimpleText(0,1,1,"My Inventory?"); // Top screen, tile x = 1, y = 1...				PA_WaitForVBL();  	}}


Obviously I can't compile this lot to check since it's specific to ds development but I hope this helps a little.

One last thing to note is that you have no way to quit, this is fine for most console development so don't worry about it but you might want to give yourself some kind of feedback once you've got this compiling ;)

I'm at work right now but will try to respond again if you need my help.

Andy

"Ars longa, vita brevis, occasio praeceps, experimentum periculosum, iudicium difficile"

"Life is short, [the] craft long, opportunity fleeting, experiment treacherous, judgement difficult."

Quote:
#include "gfx/all_gfx.c"


Is bad. C (and C++)'s compilation model is a bit quirky, so you might want to read up on it a bit.

But briefly, the compiler processes each .c file in isolation. Everything that is #include'd into the .c file basically gets copy/pasted in, but at this stage, all the other .c files you might have are not known to the compiler.

From this, it generates an object file (.obj or .o typically).

So when all .c files have been compiled, you end up with a bunch of object files.
These are then passed to the linker, which tries to figure out in which file each symbol (function, variable, whatever) are defined, and where it's referenced from. Then it merges them all together into the final executable file.

And by then, of course, the .c and .h files are no longer relevant. (And aren't 'loaded' by the program)

the point to note is that because each .c file is compiled individually, and #include just copy/pastes content from one file into another, you should never #include a .c file. It will essentially get compiled twice. Once on its own, as a .c file that is passed to the compiler, and once wherever it got #include'd.

And then you get funny errors. :)

This topic is closed to new replies.

Advertisement