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.

C/C++ > Stupid newbie question

#129 - Alunze - Fri Jan 03, 2003 7:56 pm

Here's the little problem I have: I wrote two libraries for different purposes, and both need some trigonometry in them, so I created a sincos.h file with an array of precalculated sine/cosine values for them. Then, when I try to link them together with the main file, the linker refuses with a duplicate definition (the array) error.
I know what this error means and the cause, I could copy the arrays to the lbreries as a quick fix, but I feel this is not the way to do this. I have the some problems with other libraries (for example, the gba.h file has some variable declarations in it, so linking two separate files which uses it results in the same error). Is there any way or technique to avoid this? Or is there a way to link the files so the linker doesn't comply?

Thanks.

#131 - Alunze - Fri Jan 03, 2003 8:02 pm

Sorry, this should have been post in the C/C++ coding section... Someone please move this post.

#137 - Touchstone - Fri Jan 03, 2003 8:48 pm

Actually, moving the array to the library _is_ the right way of doing it. Or to put it in another way, you should make sure that there is only one copy of the array in all of your code files or the linker will get confused.

You could make the array static, that way the symbol won't be exported out of the code file and cannot be confused with any other symbols with the same name but on the other hand you end up with lots of redundant data.

Put the array in a C file that you include in your project and then declare the array as 'extern' in your header file, that way everything will work out just fine and you won't have any redundant data.

#144 - Lord Graga - Fri Jan 03, 2003 10:48 pm

Hmm....
Maybe you included the .h file in BOTH lib's.
Also, SIN and COS are standard functions in C (as far as i remember), maybe you should try to rename them.

#156 - zeuhl - Sat Jan 04, 2003 1:28 am

Alunze wrote:
Here's the little problem I have: I wrote two libraries for different purposes, and both need some trigonometry in them, so I created a sincos.h file with an array of precalculated sine/cosine values for them. Then, when I try to link them together with the main file, the linker refuses with a duplicate definition (the array) error.
I know what this error means and the cause, I could copy the arrays to the lbreries as a quick fix, but I feel this is not the way to do this. I have the some problems with other libraries (for example, the gba.h file has some variable declarations in it, so linking two separate files which uses it results in the same error). Is there any way or technique to avoid this? Or is there a way to link the files so the linker doesn't comply?

Thanks.


Files with .h suffix are only for prototype and global variables declarations, you know. No code or defined variables should be present in such files.

the best way to do what you need is the following :

create a "sincos.c" file, with your sin/cos array in it. suppose you have declared your sin/cos array this way :

Code:
u16 sincos[2048] = { 0x0000, 0x0001, ..... };


then in the "sincos.h" file you should put this line :

Code:
extern u16 sincos[];


then, add sincos.c to your project and include sincos.h in the files which require the sin/cos stuff.

"extern" is meant to indicate the array exists, but does not actually define it. the only place the array is defined is in "sincos.c". So, there won't be any definition conflicts anymore.

[/b]

#497 - notron - Wed Jan 08, 2003 4:32 am

Well, if you must put defined vars in an .h file, here is a way although a bit complicated.

First, define the following logic in the .h file that will contain the var:
Code:

#ifdef __myglobals__
#define EXT
#undef __EXT
#else
#define EXT extern
#define __EXT
#endif

EXT myvar[]
#ifndef __EXT
= { ......., ..... ,.... ,.....}
#endif
;


Now, you can include this .h file in all your .c/.cpp files at will. In the one .c file that has your main program, just add the following coding

Code:

#define __myglobals__


Presto, you will have only one copy of the defined variable in your object files with all the rest having a defined external for the var. A better way is to include the first set of conditional logic above in a master .h file which has all the <includes> for all your other header files. Then you only have to have the master conditional logic in one place. After that you can just use the EXT preface-construct in any of the included header files. Just don't forget to put the one #define of __myglobals__ in your main .c file BEFORE your reference to all the <include> files.
_________________
MysticX is The Defender