Page 1 of 1

Some problems with code compilation

Posted: Sat Jan 23, 2016 4:39 pm
by alko
For some reason if prescribe thus

Code: Select all

#include <genesis.h>
#include "lyapanimate.h"
#include "globalvar.h"
#include "gfx.h"

 struct myLyap
{
    FIX16 x; 
    FIX16 y; 
    FIX16 dx;
    FIX16 dy;
} lyap[16];
errors:

Code: Select all

||=== Build: default in agressor (compiler: SGDK) ===|
src\lyapanimate.c|11|error: syntax error before "FIX16"|
src\lyapanimate.c|11|warning: no semicolon at end of struct or union|
src\lyapanimate.c|12|warning: type defaults to `int' in declaration of `y'|
src\lyapanimate.c|12|warning: data definition has no type or storage class|
src\lyapanimate.c|13|error: syntax error before "dx"|
src\lyapanimate.c|13|warning: type defaults to `int' in declaration of `dx'|
src\lyapanimate.c|13|warning: data definition has no type or storage class|
src\lyapanimate.c|14|error: syntax error before "dy"|
src\lyapanimate.c|14|warning: type defaults to `int' in declaration of `dy'|
src\lyapanimate.c|14|warning: data definition has no type or storage class|
src\lyapanimate.c|15|warning: type defaults to `int' in declaration of `lyap'|
src\lyapanimate.c|15|warning: data definition has no type or storage class|
src\lyapanimate.c||In function `lyap_anim':|
src\lyapanimate.c|29|warning: passing arg 2 of `SPR_initSprite' discards qualifiers from pointer target type|
src\lyapanimate.c|43|error: request for member `x' in something not a structure or union|
src\lyapanimate.c|44|error: request for member `y' in something not a structure or union|
src\lyapanimate.c|45|error: request for member `dx' in something not a structure or union|
src\lyapanimate.c|46|error: request for member `dy' in something not a structure or union|
src\lyapanimate.c|47|error: request for member `x' in something not a structure or union|
src\lyapanimate.c|47|error: request for member `y' in something not a structure or union|
src\lyapanimate.c|55|error: request for member `x' in something not a structure or union|
src\lyapanimate.c|55|error: request for member `dx' in something not a structure or union|
src\lyapanimate.c|56|error: request for member `y' in something not a structure or union|
src\lyapanimate.c|56|error: request for member `dy' in something not a structure or union|
src\lyapanimate.c|57|error: request for member `dy' in something not a structure or union|
src\lyapanimate.c|59|error: request for member `x' in something not a structure or union|
src\lyapanimate.c|59|error: request for member `y' in something not a structure or union|
src\lyapanimate.c||In function `rain':|
src\lyapanimate.c|101|warning: passing arg 2 of `SPR_initSprite' discards qualifiers from pointer target type|
||=== Build failed: 16 error(s), 11 warning(s) (0 minute(s), 0 second(s)) ===|
But if declare variables

Code: Select all

#include <genesis.h>
#include "lyapanimate.h"
#include "globalvar.h"
#include "gfx.h"

 struct myLyap
{
    int x; 
    int y; 
    int dx;
    int dy;
} lyap[16];
compiles without errors...

Re: Some problems with code compilation

Posted: Sat Jan 23, 2016 6:00 pm
by dub
Try u16 or s16. FIX16 is a function to change variable cast.

u16 x;
.....

Re: Some problems with code compilation

Posted: Sat Jan 23, 2016 6:55 pm
by alko
oops...

FIX16() - function
fix16 - variable type.

Re: Some problems with code compilation

Posted: Sat Jan 23, 2016 7:17 pm
by alko
But that is not all.

There is another problem.

in globalvar.h i'm declare global variables.

Code: Select all

#ifndef GLOBALVAR_H_INCLUDED
#define GLOBALVAR_H_INCLUDED

#define  bul u8

//уровни

#define lvl_first 1
#define lvl_worm_boss 2
#define lvl_jaws_boss 3
#define lvl_psevdo3d 4
#define lvl_rebro 5
#define lvl_rotor 6
#define lvl_levi_boss 7

//u16 hscroll[225];// = {0}; //scroll values

u16 palette[64];

u16 ind;

Sprite sprites[80]; // 0-9 цепь 10-челюсти, головка червя 11-27 брызги ГАВНА 28- ракетк0 29- мячЪ 30-80- дождь

struct myBall
{
fix16 x;
fix16 y;
fix16 dx;
fix16 dy;
fix16 speed;
} bball;

int batx=180;
int baty=185;
int batdx=0;
int batspeed= 14;
int batcount=0;

#endif // GLOBALVAR_H_INCLUDED
the compiler reports an error related with bat variables (batx, baty, etc.)
but these variables are not declared anywhere else.

Code: Select all


Running command: make.exe -f E:\sgdk112\makefile.gen
E:/sgdk112/bin/gcc -BE:/sgdk112/bin -n -T E:/sgdk112/md.ld -nostdlib out/sega.o @out/cmd_ E:/sgdk112/lib/libmd.a E:/sgdk112/lib/libgcc.a -o out/rom.out
out/src/leviathan.o:(.data+0x0): multiple definition of `batx'
out/src/jaws.o:(.data+0x0): first defined here
out/src/leviathan.o:(.data+0x4): multiple definition of `baty'
out/src/jaws.o:(.data+0x4): first defined here
out/src/leviathan.o:(.bss+0x0): multiple definition of `batdx'
out/src/jaws.o:(.bss+0x0): first defined here
out/src/leviathan.o:(.data+0x8): multiple definition of `batspeed'
out/src/jaws.o:(.data+0x8): first defined here
out/src/leviathan.o:(.bss+0x4): multiple definition of `batcount'
out/src/jaws.o:(.bss+0x4): first defined here
out/src/lyapanimate.o:(.data+0x0): multiple definition of `batx'
out/src/jaws.o:(.data+0x0): first defined here
out/src/lyapanimate.o:(.data+0x4): multiple definition of `baty'
out/src/jaws.o:(.data+0x4): first defined here
out/src/lyapanimate.o:(.bss+0x0): multiple definition of `batdx'
out/src/jaws.o:(.bss+0x0): first defined here
out/src/lyapanimate.o:(.data+0x8): multiple definition of `batspeed'
out/src/jaws.o:(.data+0x8): first defined here
out/src/lyapanimate.o:(.bss+0x4): multiple definition of `batcount'
out/src/jaws.o:(.bss+0x4): first defined here
out/src/main.o:(.data+0x0): multiple definition of `batx'
out/src/jaws.o:(.data+0x0): first defined here
out/src/main.o:(.data+0x4): multiple definition of `baty'
out/src/jaws.o:(.data+0x4): first defined here
out/src/main.o:(.bss+0x0): multiple definition of `batdx'
out/src/jaws.o:(.bss+0x0): first defined here
out/src/main.o:(.data+0x8): multiple definition of `batspeed'
out/src/jaws.o:(.data+0x8): first defined here
out/src/main.o:(.bss+0x4): multiple definition of `batcount'
out/src/jaws.o:(.bss+0x4): first defined here
make: *** [out/rom.out] Error 1
Process terminated with status 2 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
 

Re: Some problems with code compilation

Posted: Sat Jan 23, 2016 7:20 pm
by alko
I tried to remove the temporary files *.о and rebuild.
all the same.

Re: Some problems with code compilation

Posted: Sat Jan 23, 2016 10:15 pm
by Stef
fix16 shouldn't be f16 instead ?

Re: Some problems with code compilation

Posted: Sun Jan 24, 2016 9:26 am
by dub
Your global var error is certainly because you include your .h file in every file (.c or .h). So the compiler create a new variable each time.

2 solutions : include your .h only one time or put "extern" before you variable so the compiler keep only one.

extern int batx=180;

Re: Some problems with code compilation

Posted: Sun Jan 24, 2016 10:50 am
by alko
dub wrote:include your .h only one time
I need in each module global variables.

dub wrote:or put "extern" before you variable so the compiler keep only one.
It did not help. Still the same error.

Re: Some problems with code compilation

Posted: Sun Jan 24, 2016 12:49 pm
by dub
I'm sorry, I'm not on my computer.
You can try with "static"

static int batx=180;

Re: Some problems with code compilation

Posted: Sun Jan 24, 2016 1:16 pm
by r57shell
Usage of global variables in C for noobs:
1) define variable in single header (*.h) with extern keyword.
2) define variable in single source (*.cpp) without extern keyword.

If you wanna have different variables with same name in different sources (*.cpp) you should use static keyword.
If you wanna define constant, use const keyword (if your compiller does accept it) or use preprocessor command #define.
If you wanna use expression in #define, recomended to put it into braсkets. For example

Code: Select all

#define QWE 4+3 /* NOT RECOMENDED */
#define QWE (4+3) /* GOOD */

Re: Some problems with code compilation

Posted: Sun Jan 24, 2016 1:20 pm
by Stef
alko wrote:
dub wrote:include your .h only one time
I need in each module global variables.

dub wrote:or put "extern" before you variable so the compiler keep only one.
It did not help. Still the same error.
Ohh i didn't aw you declared your variables in the .h file, that is not a good idea !
Normally you always have to allocate your variable in .c file then just do simple "declaration" in .h file by adding "extern" keyword right before as Dub told you.

See how i did in SGDK:
vdp_pal.h

Code: Select all

extern const u16 palette_grey[16];
vdp_pal.c

Code: Select all

const u16 palette_grey[16] = {...};
Also it's a good practice to protect from multiple interpretation of your header file by adding this at the beginning of the .h file:

Code: Select all

#ifndef _VDP_PAL_H_
#define _VDP_PAL_H_
and this at the end of the file:

Code: Select all

#endif // _VDP_PAL_H_
So if some variables really need to be declared/allocated in the .h file, it prevents the compiler to do multiple declaration of the variables.
Of course you need to replace _VDP_PAL_H by your own file identifier, it should be uniq.

edit:r57shell was faster :p