By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,426 Members | 1,757 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,426 IT Pros & Developers. It's quick & easy.

Access to extern struct in other header files

P: n/a
Hi all,

I can't figure out why a can't use my struct TestStructure in other
files. Any ideas? My project consists of
main.c,lib1.h,lib1.c,lib2.c,lib2.h .

main.c
-------------------------------------
#include "lib1.h"

void main(void)
{
MyFunction(ts);
}
-------------------------------------

lib1.h
-------------------------------------
#include "lib2.h"

void MyFunction(struct TestStructure *ts);
-------------------------------------

lib1.c
-------------------------------------
#include <stdio.h>
#include "lib1.h"

void MyFunction(struct TestStructure *ts)
{
printf("%d",ts->a);
}
-------------------------------------

lib2.h
-------------------------------------
extern struct TestStructure
{
int a;
int b;
};

lib2.c
-------------------------------------
#include "lib2.h"
//nothing else
-------------------------------------

Nov 21 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Steffen Loringer <st**************@freenet.de> wrote:
I can't figure out why a can't use my struct TestStructure in other
files. Any ideas? My project consists of
main.c,lib1.h,lib1.c,lib2.c,lib2.h .

main.c
-------------------------------------
#include "lib1.h"

void main(void)
Ahem! That'll be int main(void), please.
{
MyFunction(ts);
}


You never define any object called ts. You define functions taking
_parameters_ called ts, but main() is (obviously) not one of those, and
you never define it anywhere else, either.

Richard
Nov 21 '05 #2

P: n/a
Steffen Loringer <st**************@freenet.de> writes:
[...]
extern struct TestStructure
{
int a;
int b;
};

[...]

What is this supposed to do? The "extern" keyword is used for an
object or function declaration, not for a type definition. If you
just want to declare the type, drop the "extern".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 21 '05 #3

P: n/a
>I can't figure out why a can't use my struct TestStructure in other
files. Any ideas? My project consists of
You cannot extern structure definitions.

You can extern variables that have a type of that structure, but
you don't declare any variables of type "struct TestStructure",
although you do have function parameters of type "pointer to struct
TestStructure".

The variable used in main() named ts is not declared and
not initialized. It should be of type struct TestStructure * .

Gordon L. Burditt

main.c,lib1.h,lib1.c,lib2.c,lib2.h .

main.c
-------------------------------------
#include "lib1.h"

void main(void)
{
MyFunction(ts);
}
-------------------------------------

lib1.h
-------------------------------------
#include "lib2.h"

void MyFunction(struct TestStructure *ts);
-------------------------------------

lib1.c
-------------------------------------
#include <stdio.h>
#include "lib1.h"

void MyFunction(struct TestStructure *ts)
{
printf("%d",ts->a);
}
-------------------------------------

lib2.h
-------------------------------------
extern struct TestStructure
{
int a;
int b;
};

lib2.c
-------------------------------------
#include "lib2.h"
//nothing else
-------------------------------------

Nov 21 '05 #4

P: n/a

Steffen Loringer wrote:
Hi all,

I can't figure out why a can't use my struct TestStructure in other
files. Any ideas? My project consists of
main.c,lib1.h,lib1.c,lib2.c,lib2.h .


Wrong understanding of extern:

main.c
-------------------------------------
#include "lib1.h"

void main(void)
{
MyFunction(ts);
}
-------------------------------------

lib1.h
-------------------------------------
#include "lib2.h"

void MyFunction(struct TestStructure *ts);
-------------------------------------

lib1.c
-------------------------------------
#include <stdio.h>
#include "lib1.h"

void MyFunction(struct TestStructure *ts)
{
printf("%d",ts->a);
}
-------------------------------------

lib2.h
-------------------------------------
struct TestStructure
{
int a;
int b;
};

lib2.c
-------------------------------------
#include "lib2.h"
//nothing else
-------------------------------------

Nov 23 '05 #5

P: n/a
sl*******@yahoo.com <sl*******@gmail.com> wrote:
void main(void)


Don't post code like this to comp.lang.c if being taken seriously
matters to you.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 23 '05 #6

P: n/a
Steffen Loringer wrote:

Hi all,

I can't figure out why a can't use my struct TestStructure in other
files. Any ideas? My project consists of
main.c,lib1.h,lib1.c,lib2.c,lib2.h .

main.c
-------------------------------------
#include "lib1.h"

void main(void)
{
MyFunction(ts);
}
-------------------------------------

lib1.h
-------------------------------------
#include "lib2.h"

void MyFunction(struct TestStructure *ts);
-------------------------------------

lib1.c
-------------------------------------
#include <stdio.h>
#include "lib1.h"

void MyFunction(struct TestStructure *ts)
{
printf("%d",ts->a);
}
-------------------------------------

lib2.h
-------------------------------------
extern struct TestStructure
{
int a;
int b;
};

lib2.c
-------------------------------------
#include "lib2.h"
//nothing else
-------------------------------------

/* BEGIN new.c */

#include "lib1.h"

int main(void)
{
struct TestStructure ts = {0};

MyFunction(&ts);
return 0;
}

/* END new.c */
/* BEGIN lib1.c */

#include <stdio.h>
#include "lib1.h"

void MyFunction(struct TestStructure *ts)
{
printf("%d\n", ts->a);
}

/* END lib1.c */
/* BEGIN lib1.h */

#ifndef H_LIB1
#define H_LIB1

#include "lib2.h"

void MyFunction(struct TestStructure *ts);

#endif

/* END lib1.h */
/* BEGIN lib2.c */

#include "lib2.h"

/* END lib2.c */
/* BEGIN lib2.h */

#ifndef H_LIB2
#define H_LIB2

struct TestStructure
{
int a;
int b;
};

#endif

/* END lib2.h */
--
pete
Nov 23 '05 #7

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
void main(void)
Don't post code like this to comp.lang.c if being taken seriously
matters to you.


My apologies, I didn't notice that this was in fact OP's code.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 23 '05 #8

P: n/a
Christopher Benson-Manica wrote:

Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
void main(void)

Don't post code like this to comp.lang.c if being taken seriously
matters to you.


My apologies, I didn't notice that this was in fact OP's code.


I don't have any problem holding people accountable
for the code they post,
copied or not.

--
pete
Nov 23 '05 #9

P: n/a
pete <pf*****@mindspring.com> wrote:
Steffen Loringer wrote:

/* BEGIN lib1.c */

#include <stdio.h>
#include "lib1.h"

IMHO, there is a

#include "lib2.h"

missing here. But I may be wrong, and was wondering if all of you missed
this or I forgott how C works during my last project in sales.
void MyFunction(struct TestStructure *ts)
{
printf("%d\n", ts->a);
}

/* END lib1.c */


--
Z (zo**********@web.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 23 '05 #10

P: n/a
Zoran Cutura wrote:

pete <pf*****@mindspring.com> wrote:
Steffen Loringer wrote:

/* BEGIN lib1.c */

#include <stdio.h>
#include "lib1.h"


IMHO, there is a

#include "lib2.h"

missing here.


That line, is in lib1.h

--
pete
Nov 23 '05 #11

P: n/a
pete <pf*****@mindspring.com> wrote:
Zoran Cutura wrote:

pete <pf*****@mindspring.com> wrote:
> Steffen Loringer wrote:
>
> /* BEGIN lib1.c */
>
> #include <stdio.h>
> #include "lib1.h"
>


IMHO, there is a

#include "lib2.h"

missing here.


That line, is in lib1.h


Uh, oh, must be due to the fact that I refuse to include headers in
headers (at least in code like this) that I didn't get it at the first
sight. Thanks a lot.

--
Z (zo**********@web.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 23 '05 #12

P: n/a
Zoran Cutura wrote:
Uh, oh, must be due to the fact that I refuse to include headers in
headers (at least in code like this) that I didn't get it at the first
sight. Thanks a lot.


I don't think it's all that uncommon
for several c files in the same program
to define functions with the same user defined parameter type,
which would be a case
when headers would naturally include other headers.

I think it make most sense to assume that lib2.c
represents a file where the struct TestStructure is used,
even though lib2.c was posted as function
which didn't make any real use of it's own header.

--
pete
Nov 23 '05 #13

P: n/a
pete <pf*****@mindspring.com> wrote:
Zoran Cutura wrote:
Uh, oh, must be due to the fact that I refuse to include headers in
headers (at least in code like this) that I didn't get it at the first
sight. Thanks a lot.


I don't think it's all that uncommon
for several c files in the same program
to define functions with the same user defined parameter type,
which would be a case
when headers would naturally include other headers.


I can see your point, but I have seen programmers (including myself)
getting confused about inclusion of headers they never actually included
in their program.

I do include headers that declare "global" typedefinitions or macros in
other headers occasionally, usally when I need to hide certain
declarations from the consuming program code. When there's need for a
library or implementation of specific modules or hardware abstraction
to hide the data type this can be helpful. But if the consuming
source file needs to know the type I find it better to explicitly
include the header that declares it.

--
Z (zo**********@web.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 25 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.