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.

Coding > multiple c files

#25810 - hakanyuksel - Sat Aug 28, 2004 10:56 pm

I am using HAM. I have several c files in my project. I have a problem with calling functions from other c files. my code is like this

this is the main c file:
Code:


#include <math.h>
#include "variable.h"
#include "interrupt.h"
#include "init.h"

int main(void)
{
    init_game();

    while(1)
    {
    }
return(0);
}


this it the header of the init.c file

Code:

void init_game();



and this is the init.c file
Code:

#include "variable.h"
#include "data.h"
#include "display.h"
#include "sprites.h"
#include "init.h"

void init_game()
{
//   init_vars();

    copy_data_to_vram();

    u16 counter=0;
    // SPRITE lar sifirlanir
    for(counter=0;counter<128;counter++)
    {
        sprite_attributes[counter].attribute1 = 160;
        sprite_attributes[counter].attribute2 = 240;
        sprite_attributes[counter].attribute3 = 0;
    }

    set_video_mode( MODE_0 | SPRITES | SPR_1D | BG0 );
    BG0_SET = 128+512+1024+2048+4096;
//    init_objects();
//    write_number_16_bg0(65535,1,1,32);
}



when i compile the project I have an error like this
main.o(.text+0x1c): In function `main':
: undefined reference to `init_game()'
make: *** [engine.elf] Error 1

What am i doing wrong. Why the compiler cant find the init_game procedure. it is declared in the init.h header file....
_________________
--------------------
Hakan Yuksel
3TE GAmes
www.3tegames.com

#25811 - sajiimori - Sat Aug 28, 2004 11:06 pm

Perhaps init.c is not being compiled and/or linked in. I don't know the HAM IDE, but here's how you'd do it from the command line:
Code:

gcc -c main.c
gcc -c init.c
gcc -o out.elf main.o init.o

#25815 - hakanyuksel - Sun Aug 29, 2004 12:08 am

sajiimori wrote:
Perhaps init.c is not being compiled and/or linked in. I don't know the HAM IDE, but here's how you'd do it from the command line:
Code:

gcc -c main.c
gcc -c init.c
gcc -o out.elf main.o init.o


No it is compiling and linking all the c files. It is doing something like you say. It is a long command line so i dont copy and paste it down here..
_________________
--------------------
Hakan Yuksel
3TE GAmes
www.3tegames.com

#25816 - sajiimori - Sun Aug 29, 2004 12:29 am

Well, there are no other visible problems. All I can tell you is the header is not part of the problem because it's a linker error, and the linker doesn't care about headers.

Maybe there are some sample HAM projects with multiple .c files that you can look at. Otherwise, most people here can help you with regular command line stuff.

#25817 - hakanyuksel - Sun Aug 29, 2004 12:34 am

I am trying devkitadv now. Maybe there is a bug in HAM.
_________________
--------------------
Hakan Yuksel
3TE GAmes
www.3tegames.com

#25819 - Lord Graga - Sun Aug 29, 2004 12:52 am

I heard your prayer, fella!

#25820 - hakanyuksel - Sun Aug 29, 2004 1:28 am

OK. I solved the problem.

My project was a c++ project. It appears that you cant use c files in c++ projects(my main file was main cpp other files were c files) in HAM. I changed it into a c project and the problem is soved. I think there must be a warning when I wanted to add c files to c++ projetcs.

Thank you all
_________________
--------------------
Hakan Yuksel
3TE GAmes
www.3tegames.com

#25825 - sajiimori - Sun Aug 29, 2004 2:35 am

It's not necessarily wrong to combine C and C++ files, but there are issues to be aware of. The one you were probably running into is "name mangling". The C++ compiler changes the names of functions to allow overloading based on argument types. Here's how you specify (in C++) that a function's name is not mangled (e.g. if it was compiled by a C compiler):
Code:

extern "C"
{
  void my_function();
}

To make a module work in both C and C++ applications, arrange its header like this:
Code:

#ifdef __cplusplus
extern "C"
{
#endif

<regular header contents>

#ifdef __cplusplus
}
#endif

#25842 - hakanyuksel - Sun Aug 29, 2004 12:43 pm

I see. Thanks for the information
_________________
--------------------
Hakan Yuksel
3TE GAmes
www.3tegames.com