compile error: storage size of 'var' isn't known

G

Guest

Hello everyone,


I get a strange compile error when compile such simple program. Any ideas?

foo.c: In function `main':
foo.c:5: error: storage size of 'var' isn't known

foo.c

Code:
#include "goo.h"

int main (int argc, char** argv)
{
t_st var;
var.member = 100;
return 0;
}

goo.c

Code:
struct st {

int member;

};

goo.h

Code:
typedef struct st t_st;


thanks in advance,
George
 
B

Bo Persson

George wrote:
:: Hello everyone,
::
::
:: I get a strange compile error when compile such simple program. Any
:: ideas?
::
:: foo.c: In function `main':
:: foo.c:5: error: storage size of 'var' isn't known

The compiler can figure out that 'var' is of type 't_st', which is another
name for a struct named 'st'. However, it cannot see what 'st' is, as it is
hidden in another translation unit.

You can have a pointer to st without knowing exactly what it is, but you
cannot have a variable of type st without seeing the full definition.


Bo Persson

::
:: foo.c
::
::
Code:
:: #include "goo.h"
::
:: int main (int argc, char** argv)
:: {
::    t_st var;
::    var.member = 100;
::    return 0;
:: }
::
::
:: goo.c
::
::
Code:
:: struct st {
::
:: int member;
::
:: };
::
::
::
:: goo.h
::
::
Code:
:: typedef struct st t_st;
::
::
::
:: thanks in advance,
:: George
 
G

Guest

Thanks Bo Persson!


I want to confirm that, if I define a struct in .c file, I can only use the
struct in the same .c file, and have no walk-around to utilize the struct in
other .c file.

If I want to use the struct in multiple .c files, I have to define it in .h
file?


regards,
George
 
B

Bo Persson

George wrote:
:: Thanks Bo Persson!
::
::
:: I want to confirm that, if I define a struct in .c file, I can only
:: use the struct in the same .c file, and have no walk-around to
:: utilize the struct in other .c file.

The "work-around" is to define the struct identically in both .c files. That
in effect simulates what happens when you include a .h file in both .c
files.

Not recommended! :)

::
:: If I want to use the struct in multiple .c files, I have to define
:: it in .h file?

This is the reason for having .h files in the first place. It lets you share
declarations between several implementation files.


Bo Persson
 

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

Top