473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Typedef and external linkage

Hi,

I need a refrence of a typedef'ed structure in a file in which it is
not defined. I cannot include the header file in which the typedef is
defined.

file_a.h
----------
typedef struct dummy
{

...
}struct_a;

-------------------------------------------------------

file_b.h
----------
void foo(struct_a var1);
-------------------------------------------------------

How do I let the compiler/linker know that struct_a is defined
somewhere else. I cannot include file_a.h in file_b.h as both the files
are included in many .c files.

Nov 14 '05 #1
6 15424
pa**********@gmail.com wrote:
I cannot include file_a.h in file_b.h as both the files
are included in many .c files.


The inclusion of both .h files in many .c files,
shouldn't be a problem.

/* BEGIN file_a.h */

#ifndef H_FILE_A
#define H_FILE_A

typedef struct dummy
{

...
}struct_a;

#endif

/* END file_a.h */

--
pete
Nov 14 '05 #2

pete wrote:
pa**********@gmail.com wrote:
I cannot include file_a.h in file_b.h as both the files
are included in many .c files.
The inclusion of both .h files in many .c files,
shouldn't be a problem.


Thanks for the solution. Is there a method which doesn't include
including file_a.h in file_b.h. Can extern or a forward declaration be
used?

-Parag


/* BEGIN file_a.h */

#ifndef H_FILE_A
#define H_FILE_A

typedef struct dummy
{

...
}struct_a;

#endif

/* END file_a.h */

--
pete


Nov 14 '05 #3
In article <11**********************@g14g2000cwa.googlegroups .com>,
<pa**********@gmail.com> wrote:
pete wrote:
pa**********@gmail.com wrote:
> I cannot include file_a.h in file_b.h as both the files
Thanks for the solution. Is there a method which doesn't include
including file_a.h in file_b.h.
Put the definition in file_c.h and #include that in file_a.h
and file_b.h
Can extern or a forward declaration be
used?


No. The compiler needs the structure definition in the
compilation unit it is in. For example if structure
member next is a pointer to a character then
next - 1 would be an unsigned subtraction of 1 byte
If it is pointer to an integer, then next - 1 would
be an unsigned subtraction of as many bytes as an integer
occupies. If it is a void then next - 1 should be a compilation
error. If next is a double then you have a floating point
subtraction, if it is an integer then you have a signed
integer subtraction, and so on. Without the types of the
fields, it can't know what code to put in.
--
Look out, there are llamas!
Nov 14 '05 #4
Got it, but I am not using the struct members in the header, I just
need the type name, in the implementation file, lets call it file_b.c,
I am including file_a.h, and the function (foo) needs to be expoerted
to other modules so I needs its declaration in file_b.h.

So I was thinking that a forward declaration might help.

-Parag

Nov 14 '05 #5
In article <11**********************@g14g2000cwa.googlegroups .com>
<pa**********@gmail.com> wrote:
Got it, but I am not using the struct members in the header, I just
need the type name ...


Typedef names have no linkage. They have little practical use
anyway; the only time a typedef is actually required is in some
relatively obscure corner cases of the va_arg() macro from <stdarg.h>.

Just stop using the typedefs. Write "struct <structure-name>";
then the whole problem goes away:

file1.c:
struct zorg { ... }; /* declares and defines the structure type */
/* code that uses the actual structure */

file2.c:
struct zorg; /* declares the type without defining it */

extern struct zorg *new_zorg(void);
extern void act_on_zorg(struct zorg *);
extern voide destroy_zorg(struct zorg *);

void intermediate_func(struct zorg *p) {
... do some stuff ...
act_on_zorg(p);
... do some more stuff ...
}

void top_level_func(void) {
struct zorg *z;
...
z = new_zorg();
intermediate_func(z);
destroy_zorg(z);
}

Note that file2.c never needs to know what is inside a "struct
zorg", and no typedefs clutter up the cleanliness of using C's
type-defining and type-referring keyword "struct", which clearly
:-) stands for "STRange spelling for User-defined abstraCt Type".

(For English-speakers, just pretend it *is* the word "type" and
it will all make sense. Or:

#define type struct

type zorg;

extern type zorg *new_zorg(void);

See? :-) )
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #6
Not using typedefs is not an option. The system I am currently working
on has typedefs all over the place and used liberally. I have to live
with it and find a solution using typedefs.

-Parag

Nov 14 '05 #7

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

Similar topics

47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
4
by: Tron Thomas | last post by:
I have read that anonymous namespaces are preferred in C++ over declaring global statics because the namespace can hide information from other translation units and at the same time provide...
9
by: maxw_cc | last post by:
Hi to all, To explain my question I'll help myself with the following two code snippets: /*** file1.c ***/ #include <stdio.h> void print_it(void);
19
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...
4
by: Peter Ammon | last post by:
I would like to share a variable between two functions defined in two separate source files; no other functions will need the global variable so I'd prefer to not give it file scope. Thus, I want...
3
by: al.cpwn | last post by:
do static and inline functions or members have internal linkage? I have been reading this newsgroup on google and found conflicting ideas. Can someone please help me understand why in some places...
4
by: red floyd | last post by:
What is the linkage of a namespace scope typedef? Is it internal or external? 3.5 is unclear on this. In other words, is the following program, consisting of translation units a.cpp and...
1
by: Kurt | last post by:
The C++ standard (in 3.5:6, p42Example) said: static int i = 0; // 1 void g() { int i; //2: i has no linkage { extern int i; //3: external linkage
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.