static union {
unsigned int whole;
struct {
unsigned short half1;
unsigned short half2;
};
};
How do I declare them in the header file so that I can use whole, half1, half2 across multiple source files? I''ve tried declaring them as extern but when I use them in another source file I get a linker error (the usual LNK2001). Any help would be great! Thanks,
Jasper
Global (used in multiple files) anonymous unions
I''m stuck! I''ve got the following anonymous union declared in one .cpp file:
Just don''t make it an anonymous union (I don''t think anonymous unions, the way you''re doing them at least, are in the standard anyway).
[Resist Windows XP''s Invasive Production Activation Technology!]
[Resist Windows XP''s Invasive Production Activation Technology!]
Using named unions is not an option. I know they''re not in the standard but I would like to use them in my program. It''s not cross-platform or anything so I don''t really care about the standard, as long as it compiles on MSVC++ 6.0. There must be some way to accomplish this. Besides throwing all the source in one file Well thanks anyway,
Jasper
Jasper
The name exists so that the extern can find its way to the named space for this. There''s no name, so there''s no way for the linker to find its way home. If using a name is not an option, you''re screwed.
Mmmm, I don''t understand the compiler can''t do that. They''re just two variables using the same memory. They both have names. So the compiler should be able to find them. The union''s just for indicating they''re using the same memory. I mean, if I put all my code in one file it''ll work. What''s the problem if I split it up into multiple files? Is there any way around this? Well, thanks for your help.
Jasper
Jasper
The keyword static means make a local copy for each .obj file that each cpp compiles to.
Similarily if you inline a function with static variables, then u may be screwed when including it in multiple cpp files.
i.e
static inline void F()
{
static int counter;
}
Besides, you should use an annoymous namespace instead of static.
Of course, it seems a redesign would be better. Why are u exposing the nameless union across cpp files??
Neglecting the standard is folly. What guarantee will your code compile on the next compiler??
Similarily if you inline a function with static variables, then u may be screwed when including it in multiple cpp files.
i.e
static inline void F()
{
static int counter;
}
Besides, you should use an annoymous namespace instead of static.
namespace{ union{ ... };}
Of course, it seems a redesign would be better. Why are u exposing the nameless union across cpp files??
Neglecting the standard is folly. What guarantee will your code compile on the next compiler??
I''ve tried using an unnamed namespace as you suggested but I still have to declare the union as static else I get compiler error C2646. I have found a workaround though, so I''ll go with that.
What I still don''t understand is why it''s so hard (impossible?) or wrong to use anonymous unions. I think they''re a valueable, logical and useful addition to the language. The fact that it''s a microsoft specific doesn''t really bother me because the code is intended for MSVC++ 6.0 not a different compiler.
If anyone can come up with a solution I''d still appreciate it, because my workaround isn''t pretty Oh well, you can''t always get what you want. Thanks for your help all!
Jasper
What I still don''t understand is why it''s so hard (impossible?) or wrong to use anonymous unions. I think they''re a valueable, logical and useful addition to the language. The fact that it''s a microsoft specific doesn''t really bother me because the code is intended for MSVC++ 6.0 not a different compiler.
If anyone can come up with a solution I''d still appreciate it, because my workaround isn''t pretty Oh well, you can''t always get what you want. Thanks for your help all!
Jasper
Sorry, annoymous unions at global scope must be declared static. The unnamed namespace does not help. My bad.
But seriously, what are u trying to do with a global unnamed union? Would enum serve your needs better?
But seriously, what are u trying to do with a global unnamed union? Would enum serve your needs better?
What I''m trying to do probably requires a little more explanation than usual. So here goes... I''m trying my luck at Gameboy Advance programming. The problem is this uses the GNU-GCC compiler and I''m not really comfortable with that (creating makefiles, debugging etc). So I decided to add some cross-compiler functionality.
The idea is to create some sort of development platform with MSVC++ 6.0 that I can use to write GBA games in. Since most of the access to hardware is done through registers (just addresses in memory), it''s mostly a question of trapping writing and reading those registers and taking appropriate action. All of these registers are defined in gba.h, a standard include file you can find in almost all the sources on gbadev.org.
I already trapped reading and writing to those registers (check out post ID 65500 I believe), but some of them overlap like in the union above. I just wanted a clean way to implement that and thought nameless unions would be a good idea.
I want to use the same references as in the header file mentioned so that the seperate gameboy code is clean and can be compiled with GNU-GCC by just removing one #define.
So I''ve solved the problem now by just changing the registers as the other one gets written to. But the code''s a little messy. Anonymous unions would be ideal for the situation (I believe).
Mmm maybe this explanation isn''t really clear... Well I hope you get the idea about the registers and why I want anonymous unions. Mainly because I don''t want to use the . in my variable names (that sums it up pretty well ). Thanks for your help anyway,
Jasper
The idea is to create some sort of development platform with MSVC++ 6.0 that I can use to write GBA games in. Since most of the access to hardware is done through registers (just addresses in memory), it''s mostly a question of trapping writing and reading those registers and taking appropriate action. All of these registers are defined in gba.h, a standard include file you can find in almost all the sources on gbadev.org.
I already trapped reading and writing to those registers (check out post ID 65500 I believe), but some of them overlap like in the union above. I just wanted a clean way to implement that and thought nameless unions would be a good idea.
I want to use the same references as in the header file mentioned so that the seperate gameboy code is clean and can be compiled with GNU-GCC by just removing one #define.
So I''ve solved the problem now by just changing the registers as the other one gets written to. But the code''s a little messy. Anonymous unions would be ideal for the situation (I believe).
Mmm maybe this explanation isn''t really clear... Well I hope you get the idea about the registers and why I want anonymous unions. Mainly because I don''t want to use the . in my variable names (that sums it up pretty well ). Thanks for your help anyway,
Jasper
I don''t understand the register part, but what u are doing is not possible.
A union is like a type declaration. A nameless union binds it to the closest scope.
And a global nameless union is binds to nothing so you do have to specify a variable. 8(
A union is like a type declaration. A nameless union binds it to the closest scope.
And a global nameless union is binds to nothing so you do have to specify a variable. 8(
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement