LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-12-2008, 02:21 PM   #1
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,962

Rep: Reputation: Disabled
g++ doesn't support "C-struct member initialization" extension of gcc?


Take the following code:
Code:
static struct
{
    int value;
} local = { .value = 0 };
This compiles with gcc but not with g++, even with extern "C". I realize that this isn't part of a language standard, but I want consistency between my C and C++ files. Additionally, I want to be explicit with member initialization to reduce the possibility of bugs with large structures. The most critical places I need this are with POSIX structures that aren't necessarily ordered the same way across different OSes (perfect example: struct flock on Linux vs. FreeBSD.)

I can't use normal C++ structures in most places I'm using this. My project's API is in C and the C-structs are returned or fed to hook functions, but are assembled by C++ code internally.
ta0kira
 
Old 04-12-2008, 02:38 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
I can't use normal C++ structures in most places I'm using this...
So I take it that all the structs are POD? by the way value will be default initialised to zero and this is not a gcc extension it is part of the C language.

Last edited by dmail; 04-12-2008 at 02:41 PM.
 
Old 04-12-2008, 03:19 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,962

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dmail View Post
So I take it that all the structs are POD? by the way value will be default initialised to zero and this is not a gcc extension it is part of the C language.
Yes, they're all pointers and integers for the most part. If it's part of C, why won't g++ compile it even when it's in a C source file? It should compile in a C++, also, if it's standard C.

With non-static data in my C++ files I'm using this:
Code:
struct hold_value
{
    int value;
};

//...

struct hold_value local = { };
local.value = 0;
That's file within functions, but doesn't help when I need, e.g., a static constant. For now I'm just "sucking it up" and leaving out the .* for those and luckily most of those structures are ones I wrote.
ta0kira

PS Comeau does compile it in C99 mode but not in C++ mode. Did they just decide to leave this out of C++?

PPS This is a project for "the masses," so I'm better off doing it the way I have in this post. Making assumptions isn't too big of a deal with the static data I have, but it does make for an irritating code inconsistency.

P3S I suppose I could just extern those statics and put them in C sources, then __attribute__ ((visibility("internal"))) them...

Last edited by ta0kira; 04-12-2008 at 03:26 PM.
 
Old 04-12-2008, 03:41 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 66
There are two syntaxes for designated initializers in the GNU Compiler Collection: the C99 one and the GNU-specific one. The C99 one will only work with gcc (and only I believe with --std={gnu,c99,gnu99}), the GNU-specific one will work with gcc and g++. The downside is that you lose portability to other C compilers which would have understood the first syntax fine (so I guess this is a trade-off which you have to consider in your project).

The syntax you used earlier is that of the C99 standard. If you want the GNU-extension equivalent, try:
Code:
struct
{
    int value;
} local = { value : 0 };
(the above should work on both gcc and g++).
 
Old 04-12-2008, 03:45 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
If it's part of C, why won't g++ compile it even when it's in a C source file? It should compile in a C++, also, if it's standard C.
Because it is not part of C++, in this language we use constructors yet some features similar to this are added with c++ox. I do not understand what you mean by "it's in a C source file", a cpp source file does not have to end with ".cpp", in fact I always use ".h" and ".c" for C++ files.

edit well I don't use ".c" but you would if you wanted.

Last edited by dmail; 04-12-2008 at 03:54 PM.
 
Old 04-12-2008, 08:32 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,962

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by osor View Post
The syntax you used earlier is that of the C99 standard. If you want the GNU-extension equivalent, try:
Code:
struct
{
    int value;
} local = { value : 0 };
(the above should work on both gcc and g++).
Yes! That's exactly what I need. No worries about it being a GNU extension since I already use __attribute__ gratuitously for security reasons. My only portability requirement is that it work on all *nixes with gcc. I'd rather impose that limitation than forgo the security provided by hiding symbols (though admittedly not all binary formats support those attributes.) Thanks!
ta0kira

Last edited by ta0kira; 04-12-2008 at 09:10 PM.
 
Old 04-12-2008, 08:59 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,962

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dmail View Post
Because it is not part of C++, in this language we use constructors yet some features similar to this are added with c++ox. I do not understand what you mean by "it's in a C source file", a cpp source file does not have to end with ".cpp", in fact I always use ".h" and ".c" for C++ files.

edit well I don't use ".c" but you would if you wanted.
Yes, I also find that to be an irritating inconsistency. When I write a C file with .h, I really mean for it to be a C file and I don't want to have to use
Code:
#ifdef __cplusplus
extern "C" {
#endif
or include it in C++ files with extern "C". The problem is that initial versions of C++ didn't have the foresight to avoid .h standard headers, so now it's a habit of the user base. Anyway, that's another story altogether.
ta0kira
 
Old 04-12-2008, 09:08 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,962

Original Poster
Rep: Reputation: Disabled
Is there any way around using extra braces to initialize unions?
Code:
const char name[] = "name";

static struct
{
	int value;
	union {
	const char  *single;
	const char **multi; };
} local = { value: 0, { single: name } };
If not, it isn't that big of a deal...
ta0kira
 
Old 04-13-2008, 02:57 PM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 66
Quote:
Originally Posted by ta0kira View Post
Is there any way around using extra braces to initialize unions?
I don’t think so.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 03:07 PM
Video Card Nvidia Driver Xlib: extension "GLX" missing on display ":0.0". HELP! badgerbox76 Linux - Newbie 35 03-08-2007 02:27 AM
Video Card Nvidia Driver Xlib: extension "GLX" missing on display ":0.0". badgerbox76 Linux - Games 13 01-11-2006 04:47 PM
suse ati driver "Xlib: extension "XiG-SUNDRY-NONSTANDARD" missing" madcow007 Linux - Hardware 0 03-06-2004 02:53 PM


All times are GMT -5. The time now is 06:17 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration