gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Using more C++ files

#75621 - patroclus02 - Tue Mar 14, 2006 7:53 pm

Hello,
I would like to split up my code into several c++ source files.
Ok, what I've done is to take some functions out to a another .c file, without writing any .h file for it, and not including the new .c file in my main unit, as I get multiple reference to its functions then.

when I compile, it works fine but I get a
warning: implicit declaration of function 'checkTouch'
in my main file...

what is the right way of doing this?
Thank you

#75625 - tepples - Tue Mar 14, 2006 8:04 pm

You put the function's prototype in the .h file.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#75627 - patroclus02 - Tue Mar 14, 2006 8:17 pm

Writing a h file or not leads to same results.
the only way it does not complain about 'implicit declaration' is

I put c file insice a include directory (defined in makefile)
I do "#include "davlib.c" in main file.

H file is not needed...
If I write a H file and include it
"#include "davlib.h" instead of "#include "davlib.c"
I get LOTS of warnings...

#75629 - tepples - Tue Mar 14, 2006 8:22 pm

patroclus02 wrote:
If I write a H file and include it
"#include "davlib.h" instead of "#include "davlib.c"
I get LOTS of warnings...

What is the exact text of the first ten warnings that you get, and what are the exact source lines of code that are listed in these warnings?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#75630 - patroclus02 - Tue Mar 14, 2006 8:29 pm

Code:

c:/David/NDS/David5/source/gfx/ball.raw.c:4: error: parameter 'ball_Bitmap' is initialized
c:/David/NDS/David5/source/gfx/ball.raw.c:5: warning: excess elements in scalar initializer
c:/David/NDS/David5/source/gfx/ball.raw.c:5: warning: (near initialization for 'ball_Bitmap')
c:/David/NDS/David5/source/gfx/ball.raw.c:5: warning: excess elements in scalar initializer
c:/David/NDS/David5/source/gfx/ball.raw.c:5: warning: (near initialization for 'ball_Bitmap')
c:/David/NDS/David5/source/gfx/ball.raw.c:5: warning: excess elements in scalar initializer


And so on. they are grpahics converted with gfx2gba.
the only way of getting it working is what I said.
Also, if I put C file in source directory along with main.c, and not write "#include davlib.c" neither use any H file, it works but I get a "implicit declaration" warning... don't knwo why

#75631 - tepples - Tue Mar 14, 2006 8:31 pm

Please paste the first five lines of ball.raw.c so that I can show you what to put in the header.

Clue: The prototype for an initialized array needs the keyword 'extern' before it.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#75632 - patroclus02 - Tue Mar 14, 2006 8:34 pm

Code:

//
// ball (256 uncompressed bytes)
//
const unsigned char ball_Bitmap[256] = {
0x00, 0x00, 0x00, 0x00, 0x06, 0x06, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x06, 0x06, 0x06, 0x06,
0x00, 0x00, 0x06, 0x06, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x06, 0x06, 0x07, 0x07, 0x07, 0x07,
0x06, 0x06, 0x07, 0x07, 0x08, 0x08, 0x08, 0x08, 0x06, 0x06, 0x07, 0x07, 0x08, 0x08, 0x08, 0x08,
0x06, 0x06, 0x07, 0x07, 0x08, 0x08, 0x08, 0x08, 0x06, 0x06, 0x07, 0x07, 0x08, 0x08, 0x08, 0x08,
0x06, 0x06, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x06, 0x06, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00,



there's no H file for this C file neither any other C file I use. this file was created with gfx2gba in SRC mode

Also it was working perfectly before I tried to split up my code in several C files. and it still works (and I get no warnings) if a don't include the H file "davlib.h".

#75634 - tepples - Tue Mar 14, 2006 8:50 pm

patroclus02 wrote:
Code:

//
// ball (256 uncompressed bytes)
//
const unsigned char ball_Bitmap[256] = {
0x00, 0x00, 0x00, 0x00, 0x06, 0x06, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x06, 0x06, 0x06, 0x06,
/*snip*/


Put the following line in the .h file for your project:
Code:
extern const unsigned char ball_Bitmap[256];

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#75636 - patroclus02 - Tue Mar 14, 2006 8:59 pm

which H file?
I tried to add it to davlib.h and also ball.raw..h, but nothing changes...

#75638 - tepples - Tue Mar 14, 2006 9:01 pm

I don't know what davlib.h is. It would be faster for me to help you if you put all your source code in a zip file and post it on the web somewhere so that I can explain the thought process that I would use to help you.

Or you can convert your images to binary and use GBFS instead of converting your images to source code.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#75645 - funkaster - Tue Mar 14, 2006 10:13 pm

patroclus02 wrote:

what is the right way of doing this?
Thank you


you define either a common header file (with all the function's signatures) or one header per .c file.
If you have global variables, you define them in just one .c file, and in the corresponding header file, you define it like this:

Code:

extern <type> <variable>;


for instance, if you have a global array of data, you could define it in the source file like this:

Code:

unsgined char myarray[1024] = {...};


and in the corresponding header like this:

Code:

extern unsigned char myarray[1024];


hth...

#75646 - patroclus02 - Tue Mar 14, 2006 10:34 pm

tepples I sent you a MP with the link to download the source code.

funkaster thanks but that is not the problem I have :)

#75653 - patroclus02 - Tue Mar 14, 2006 11:41 pm

tepples, thank you. It works.
But the extern declarations are a bit messy to include in ALL new header file, and modify all them if something changes...
. Do I need to add extern declarations if not going to use those variables in that particular C source file??

#75662 - tepples - Wed Mar 15, 2006 12:31 am

No, you don't need extern declarations for things that a given C file doesn't use. For instance, you could have one file that handles copying images into VRAM and then only put the extern declarations for VRAM data in that file.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#75674 - genfish - Wed Mar 15, 2006 3:34 am

i do it like this:

main.cpp
Code:


#include "CExample.h"

int main()
{
// code here
}



CExample.h
Code:

#ifndef CExample_h
#define CExample_h

class CExample
{
public:
int a,b;
CExample();
~CExample();

private:
bool ExampleFunc(int a);
};

#endif


CExample.cpp
Code:

CExample::CExample()
{
// constructor code here
a = 0;
b = 0;
}

CExample::~CExample()
{
// destructor code
}

bool CExample::ExampleFunc(int a)
{
// code here

return true;
}

_________________
there is no rl only afk

#75747 - patroclus02 - Wed Mar 15, 2006 6:03 pm

Is there a way of using global defines??
I need to acces to some "defines" in more than one source file.

#75763 - genfish - Wed Mar 15, 2006 8:56 pm

put them in a header and include it where the defines are required.

I keep relevant defines coupled in files with my ADT's or classes, then the defines are included whenever they are needed.
_________________
there is no rl only afk

#75825 - patroclus02 - Thu Mar 16, 2006 8:51 am

Yes, what I do is define in header file, but I also need to define un its associated C file. then all other C files that include the H header, can acces the defined data.
Is it ok?

#75842 - genfish - Thu Mar 16, 2006 3:14 pm

if you have defined something in a header file and that file is included, you do not need to define the values again in the source file
_________________
there is no rl only afk

#75859 - waruwaru - Thu Mar 16, 2006 4:58 pm

patroclus02 wrote:
Yes, what I do is define in header file, but I also need to define un its associated C file. then all other C files that include the H header, can acces the defined data.
Is it ok?


do the #ifndef trick. In each one of your .h file, wrap the entire file in
Code:

#ifndef __UNIQUEHEADERFILENAME_H__
#define __UNIQUEHEADERFILENAME_H__

// all your header goes in here
#define WHATEVER_YOU_WANT "blahblahblah"

// also declare your global vars you want to share between files with extern
extern int GLOBAL_COUNTER;

#endif // __UNIQUEHEADERFILENAME_H__


And in the cpp file where the global variable is maintained, define the global var:
Code:

int GLOBAL_COUNTER = 0;


Now all your files that needs the #define and the global variables just include this header file.
_________________
DSLua - a scripting language for your DS!

#75862 - genfish - Thu Mar 16, 2006 5:12 pm

genfish wrote:
i do it like this:

CExample.h
Code:

#ifndef CExample_h
#define CExample_h

class CExample
{
public:
int a,b;
CExample();
~CExample();

private:
bool ExampleFunc(int a);
};

#endif



:)
_________________
there is no rl only afk

#75876 - waruwaru - Thu Mar 16, 2006 7:22 pm

[quote="genfish]:)[/quote]

That's what I said! Just kidding. :) Pardon the echos.
_________________
DSLua - a scripting language for your DS!

#75882 - patroclus02 - Thu Mar 16, 2006 8:37 pm

do I always have to use the extern directive??
So far I'm declaring variables in H files without extern. it works...

#75883 - tepples - Thu Mar 16, 2006 9:00 pm

patroclus02 wrote:
do I always have to use the extern directive??
So far I'm declaring variables in H files without extern. it works...

It won't work in C version 1999, nor in C++, and it isn't reliable for initialized arrays.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#75988 - funkaster - Fri Mar 17, 2006 1:11 pm

patroclus02 wrote:
do I always have to use the extern directive??
So far I'm declaring variables in H files without extern. it works...


May I suggest you to read a good book on C?... K&R is *the* book you should read if you ever want to learn C the good way.

#75994 - genfish - Fri Mar 17, 2006 2:24 pm

This one :)

amazon
_________________
there is no rl only afk

#76039 - funkaster - Fri Mar 17, 2006 10:09 pm

genfish wrote:
This one :)

amazon


exactly, that's the bible. ;-)

#76191 - silent_code - Sun Mar 19, 2006 4:34 am

sounds like you missed some c/c++ fundamentals. .h files are like signatures or interfaces (not exactly, but one can imagine it) that shouldn't contain any implementations (real code). so, i neither know about your programming skills nor your experience, but i suggest learn using one of those two languages (programming) first. i prefer c++, but i used c a lot in the past. no matter what way you go: start with hello world and stuff and work through a book. and don't just read it, learn from it by typing the listings into your pc and compiling and executing and testing and ... everything. the error messages are the programmers best friends. there's nothing more horrible than typos, ok, but read an error message carefully (ok, skip the wierdo stuff and read the human readable parts) and they'll often direct you to the "hot spot of error".

and please: i know of NO way you are able to compile and link multi source file projects without headers to let the sources interact in some way (though i don't claim i know a lot about anything). you may also use "#pragma once" instead of the whole "#if...whatever #endif" stuff. read it up, man.
defines just need to be defined in a common header (like e.g. "common.h") and included everywhere you need them. trust me (at least a bit), it will help to prevent a few headaches. use as lot of modules (c/c++ source files) as possible! but without headers, they are woth nothing! don't include source files. in my entire programming life there's been only ONE time i needed to include a .c. i haven't checked why, but i may. this one was for university. buggy code. yea, that's how they teach us - we're the future (man, i'm just a BIT offtopic right now)... ;p

greets!