Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

Ads

#1 2021-05-25 13:42:42

pajcek
New member
Registered: 2021-05-25
Posts: 4

Multiple definition error when including .c file exported from GBTD

Hello!

I just started watching GamingMonsters tutorial videos, and I am already getting an error just trying to display a sprite.

I use the newest GBDK and GB Tile Designer. My understanding that the only difference is that the new one adds "const" to the variables.

Anyway, GBTD gives you a .c and a .h file. The tutorial only uses the .c file. I have seen GBDK examples where they use both the .c and the .h file for one sprite, and then only a .c file for another (which seems very confusing and inconsistent. The example in question is "colorbar". It is part of the GDBK package)

Here is the code:

    // MyX.c
    const unsigned char MyX[] =
    {
      0x00,0x81,0x00,0x42,0x00,0x24,0x00,0x10,
      0x00,0x08,0x00,0x24,0x00,0x42,0x00,0x81,
      0x81,0x81,0x42,0x42,0x24,0x24,0x08,0x08,
      0x10,0x10,0x24,0x24,0x42,0x42,0x81,0x81
    };

    // MyX.h
    #ifndef __MyX_h_INCLUDE
    #define __MyX_h_INCLUDE

    #define MyXBank 0

    extern const unsigned char MyX[];

    #endif

    // main.c
    #include <gb/gb.h>
    #include "MyX.c"

    void main(void)
    {

        set_sprite_data(0, 2, MyX);
        set_sprite_tile(0, 0);
        move_sprite(0, 88, 78);
        SHOW_SPRITES;

        while(1) {
            wait_vbl_done();
        }
    }

When I include only the .c file, I get this error:

    Multiple definition of _MyY
    make: *** [Makefile:29: MyGame.gb] Error 1

When I include only the .h file, everything WORKS FINE

When I include both the .c and .h files, I get the same error if .c is included after .h. Otherwise, if .c is included before .h, then there are some dangerous errors.

When I just paste the contents of the .c file before the main loop, I get the error, but if I change the name of the variable in the declaration and in set_sprite_data(), it magically works fine.

My compilation command is: `"../gbdk/"bin/lcc  -o MyGame.gb main.c MyX.c `. This is what the console says. It appears that it automatically processes the .c file, despite me not including it in my final version where I paste the contents in the mail file :curious

_____________________________________

So now that you have all the facts, can you tell me why there are a .c and a .h file, when most tutorials only include the .c file?
Why does including the .h file work, and including the .c file doesn't?
What should I do if I want to include sprites this way? Should I just inline it and have everything in one file?
Any other advice you would give a beginner?

Thank you!

Offline

 

#2 2021-05-25 16:37:43

Tauwasser
Member
Registered: 2010-10-23
Posts: 160

Re: Multiple definition error when including .c file exported from GBTD

You're not supposed to include c files in other c files if you also compile the included c files as stand-alone objects as in your example.

The multiple definition comes from the fact that you have the same symbol (MyX) in two objects, main.o and MyX.o and the linker does not know what to do with that.
Just compiling using the extern declaration you used by including the header is fine and the way it's supposed to work.
This will generate a symbol in main.o for MyX that is undefined and a symbol MyX in MyX.o and the linker will resolve the undefined symbol in main.o to the one defined in MyX.o smile

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson