473,785 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

external struct declaration

I have a Lex file containing definitions of 2 structures like:

%{
struct a {...};
struct b
{
struct a i;
...
};
struct a x[10];
struct b y[10];
....
%}

In main program I have external declarations

/* main.c */

extern struct a;
extern struct b;
extern struct a x[];
extern struct b y[];

When I try to compile and run I am getting

'Warning:Useles s keyword or type name in external declaration'
and
'Invalid use of undefined type : struct a'

Is there anything wrong in the external declarations
of the structure?
Nov 14 '05 #1
5 8431
anonymous <iu*********@ya hoo.co.in> wrote:
I have a Lex file containing definitions of 2 structures like: %{
[Hint for those not used to lex: the '%{' starts a section of
C code which goes unchanged into the C source file created from
the lex file, so the question isn't really OT.]
struct a {...};
This makes a new structure type 'a' known within that (lex) file
(but only within that file, nowhere else).
struct b
{
struct a i;
...
};
And that of the new structure type 'b'.
struct a x[10];
struct b y[10];
And here you define two arrays of structures of type 'a' and 'b'.
Everything fine so far.
In main program I have external declarations /* main.c */ extern struct a;
You can use 'extern' only when there's a variable or function name
following it. What you write here is basically like having a line like

extern int;

which simply makes doesn't make sense. That explains the first warning
'Warning:Useles s keyword or type name in external declaration'
Even worse, when the compiler reads that file it has no idea at all
what 'struct a' is or means. It doesn't know that 'struct a' has been
(or may only will be) declared in a different source file. What you
need to do is either declare again here what 'struct a' means - but
that's a rather bad idea, because it's too easy to have the decla-
rations get out of sync - or to create an include file, into which
you put what 'struct a' is, and then include it from every file that
needs to know what 'struct a' means. If you do that the second warning
'Invalid use of undefined type : struct a'
for the line
extern struct a x[];


will disappear.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #2
[...]
Even worse, when the compiler reads that file it has no idea at all
what 'struct a' is or means. It doesn't know that 'struct a' has been
(or may only will be) declared in a different source file.

[...]

But when I create the executable, both the .o files are used:

gcc -o lex.yy.o main.o

so, the definitions must be visible - right?
Nov 14 '05 #3
anonymous <iu*********@ya hoo.co.in> wrote:
[...]
Even worse, when the compiler reads that file it has no idea at all
what 'struct a' is or means. It doesn't know that 'struct a' has been
(or may only will be) declared in a different source file. [...]

But when I create the executable, both the .o files are used: gcc -o lex.yy.o main.o so, the definitions must be visible - right?


But then you're already linking. But what you got was an error
message from the compiler. And the compiler only deals with
single files at a time (if you don't count included files, but
they simple are more or less pasted in the source). So, when
the compiler sees your yy.c file (or however it's called) it
finds the definition of the structure and creates an .o file from
it without problems. But when it gets to main.c (the one where you
also use the structure), it has forgotten everything it did see in
the other file (typically, it's a completely new invocation of the
compiler) and so it has no idea what that structure is supposed to
be and complains, and you don't even get a main.o file. The easiest
way to make the type "struct a" visible to both files is to put the
definition into an additional include file that then gets included
from both files.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #4

In article <2g************ @uni-berlin.de>, Je***********@p hysik.fu-berlin.de writes:
anonymous <iu*********@ya hoo.co.in> wrote:
/* main.c */
extern struct a;
You can use 'extern' only when there's a variable or function name
following it. What you write here is basically like having a line like

extern int;

which simply makes doesn't make sense. That explains the first warning
'Warning:Useles s keyword or type name in external declaration'


Good so far.
Even worse, when the compiler reads that file it has no idea at all
what 'struct a' is or means.
Incomplete struct declarations are fine in C, provided you don't try
to use them as complete types, and in fact they're useful for
declaring opaque types in at least one context - function prototypes.

If main.c doesn't need to complete structs a and b (if it doesn't
need to do anything with their members, and doesn't need to take
their size, eg using the sizeof operator), then:

struct a;
struct b;

would be fine in main.c, to introduce the names "a" and "b" to the
struct namespace.

The only use I can think of offhand for this "bare" struct tag
declaration is before a function prototype which uses a pointer to
the struct, as in

struct a;
extern void foo(struct a *);

because there a previously-unseen struct tag (if you omitted the
"struct a;" line) will declare the structure only in prototype scope,
which is basically useless.
It doesn't know that 'struct a' has been
(or may only will be) declared in a different source file.


Which is fine, since it doesn't need to know whether any other TU
("source file") has declared struct a. That's irrelevant to the
behavior of main.c.

In this case, the OP probably wants to use struct a as a complete
type - if main.c is going to operate on that that array of struct a,
it needs to be complete - but there are APIs that use incomplete
structure definitions to create and pass opaque types safely.

--
Michael Wojcik mi************@ microfocus.com

Advertising Copy in a Second Language Dept.:
Tapestry of the encounting and the farewell, the birth and the death.
You can hear the human being's song running through the 100 years.
-- Squaresoft
Nov 14 '05 #5
Groovy hepcat Je***********@p hysik.fu-berlin.de was jivin' on 16 May
2004 10:51:23 GMT in comp.lang.c.
Re: external struct declaration's a cool scene! Dig it!
anonymous <iu*********@ya hoo.co.in> wrote:
extern struct a;


You can use 'extern' only when there's a variable or function name
following it. What you write here is basically like having a line like

extern int;

which simply makes doesn't make sense. That explains the first warning


Actually, as the compiler (probably) sees it, the errant line
declares an external object of type struct. Hence this warning:
'Warning:Useles s keyword or type name in external declaration'


"struct" (without a tag) is indeed a useless keyword or type name.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2813
by: Ida | last post by:
Hi, I am trying to build an dll with Microsoft Visual C++ but during the linking phase I get linking errors. Script.obj : error LNK2019: unresolved external symbol __imp__PyString_AsString referenced in function "public: static struct _object * __cdecl SomeFunctionName Script.obj : error LNK2019: unresolved external symbol __imp__PyObject_Repr referenced in function "public: static struct _object * __cdecl
5
8454
by: Yoon-Soo Lee | last post by:
I am using Visual C++ .NET 2003 and running into some linking error from the following template code. The error messages is error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Test<int> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Test@H@@@Z ) referenced in function _main
19
2919
by: J. J. Farrell | last post by:
After many years of dealing with definition and linkage issues in ways that I know to be safe, I've decided it's time to try to understand this area properly. Consider a header file with the file scope declaration int i; This header is included in two files that refer to i but do not declare it. The two files build together into a single program.
19
2644
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; } RtWidget;
2
5332
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: 1>make_buildinfo.obj : error LNK2019: unresolved external symbol __imp__RegQueryValueExA@24 referenced in function _make_buildinfo2 Ask on python-list@python.org . - Josiah
5
4802
by: bonnielym84 | last post by:
Im new here..didnt noe whether is this the rite way to post my problem..Really need help here..i've been stucked in this error from last wk..My problem is like this..Im using VC++ 6.0 to compile my C codes so that i could generate a dll file out of it and used in my C# program.. When I compile, got no error and no warning. But when i build it, i have these errors.. *beginning* sendrcv.obj : error LNK2001: unresolved external symbol "int...
0
1899
by: bonnielym84 | last post by:
Im new here and im not sure whether is this the right place for me to post my question..anyway..hope that you can help me..i have been stucked in this problem since last wk..My problem is..I'm using VC++6.0 to compile my C code in order to get a DLL file for my C# program..but then..when i compile and build..i get 9 errors..n here they are... *beginning* sendrcv.obj : error LNK2001: unresolved external symbol "int __cdecl CS2_RECEIVE(struct...
10
2245
by: Raman | last post by:
Hi All, Is it valid: struct test{ }; I mean, Can we have a struct containing no members? Is this a an
0
1421
by: constructor | last post by:
Hello NG, I'm having to use a function in an external library that returns a pointer to an array of structs. The function declaration according to library vendor is: device** getList( int &count) The returned structs: typedef struct tag_dev {
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.