Access to extern struct in other header files

S

Steffen Loringer

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
 
R

Richard Bos

Steffen Loringer said:
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

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
 
K

Keith Thompson

Steffen Loringer said:
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".
 
G

Gordon Burditt

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
 
S

slebetman

Steffen said:
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
 
P

pete

Steffen said:
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


/* 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 */
 
C

Christopher Benson-Manica

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.
 
P

pete

Christopher said:
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.
 
Z

Zoran Cutura

pete said:
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.
 
Z

Zoran Cutura

pete said:
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.
 
P

pete

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.
 
Z

Zoran Cutura

pete said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top