Sign in to follow this  
zaczac

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

Recommended Posts

zaczac    122
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?

Share this post


Link to post
Share on other sites
ToohrVyk    1595
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.

Share this post


Link to post
Share on other sites
zaczac    122
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!

Share this post


Link to post
Share on other sites
ToohrVyk    1595
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?

Share this post


Link to post
Share on other sites
zaczac    122
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.

Share this post


Link to post
Share on other sites
Pzc    216
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.

Share this post


Link to post
Share on other sites
NineYearCycle    1538
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

Share this post


Link to post
Share on other sites
zaczac    122
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 Files
InventoryInit(); // Game Init from game.c
LoadInventory(); // 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 Files
PA_EasyBgLoad(0, 3, inventory); //screen, background number, background name
PA_InitText(0, 1);

} // End of The Inventory Init

void 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!

Share this post


Link to post
Share on other sites
NineYearCycle    1538
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 follow

void InventoryInit(void)
{
//Load Files
PA_EasyBgLoad(0, 3, inventory); //screen, background number, background name
PA_InitText(0, 1);

} // End of The Inventory Init

void 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

Share this post


Link to post
Share on other sites
Spoonbender    1258
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. :)

Share this post


Link to post
Share on other sites
zaczac    122
Well.. Im using a library and according to the tutorials, and example files, everything im doing IS CORRECT, it compiles and exports to .nds and i can play it too.. Im just saying that is there a way to get code from main.c to continue loading even if i also load another .c file into the memory.

Share this post


Link to post
Share on other sites
NineYearCycle    1538
Quote:

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.


@Spoonbender
Woah I totally missed that one!

@zaczac
I think you've got some misunderstanding of how 'c' works. It doesn't "load" c files into memory. The compiler takes every single 'c' file in your project and compiles them (google or check wikipedia for what that means) into objects. It then takes these objects and makes the exe from them, in this case it's a ".nds" file but it's the same thing.

There is no loading of 'c' files so we're a little confused with what you actually mean about that.

I'm wondering if what you mean in the original post is that after it has started to run the "LoadInventory()" function it stops doing anything else. Is that correct?

If it is (forgive me if i'm underestimating your coding ability here) then it's possibly because that code is outside of the main game loop. To go back to your original example and modify it to explain a little see below:

void LoadInventory(void)
{
while(1) // Main game loop
{
PA_OutputSimpleText(0,1,1,"My Inventory?"); // Top screen, tile x = 1, y = 1...

// if you want anything to happen in your game -
// - then it has to happen within this loop - Andy
// for example; you could write another function like this.
UpdateThePlayer(); // this updates you players position (for example).

PA_WaitForVBL();
}
}



If that's not what you're on about then you're going to have to go into a little more detail about what you're expecting to happen.

Andy

Share this post


Link to post
Share on other sites
NineYearCycle    1538
Actually, and I might be barking up the wrong tree here but, I'm sure what you're expecting to happen is that after you call "LoadInventory()" the game continues to run in the "main()" function.

What will actually happen is that it will go into "LoadInventory()" and will loop forever because your "main game loop" is done inside that function. Since the way you've written your game loop is as a "while(1)" it will never finish. This is because 1 will never stop being 1.

This isn't a bug this is what's probably meant to be happening in the examples you're following. However you must realise that any loading code you've written must be called before this function is called and any code that you need to run must then be put _inside_ that "while(1)" loop simply because it *IS* where your entiure game actually happens.

erm, hope I'm not confusing things more!

Andy

Share this post


Link to post
Share on other sites
yaustar    1022
Quote:
Original post by zaczac
Well.. Im using a library and according to the tutorials, and example files, everything im doing IS CORRECT, it compiles and exports to .nds and i can play it too.. Im just saying that is there a way to get code from main.c to continue loading even if i also load another .c file into the memory.

Actually, any #include "something.c" is a very common mistake in some of the GBA and DS tutorials to load graphics data generated from tools. What you want is:

// gfx.c
// Define data in source files
char someData[] = { /*....*/ };




// gfx.h
// Declare data in headers
#ifndef GFX_H
#define GFX_H

extern char someData[];

#endif // GFX_H



// main.c
// Include the header in the source files that you need it in
#include "gfx.h"

int main()
{
/* game code */
}



There are two main reasons for doing this:
1. When you change the gfx data, you are only forced to recompile that one source file rather then everywhere that it is #included leading to shorter build times.

2. It stops the same data being duplicated in multiple source files where you have #included it in due to the nature of the way #include works through text replacement.

Share this post


Link to post
Share on other sites
zaczac    122
I think i get it now, it makes sense,i should of really considered how it all works. I think i understand now how to solve this problem. I'll reply back later, hopefully i have overcome this obstacle and carry on with the game.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this