Reputation: 5894
I am working with xv6 and there is a c file that contains this:
struct {
struct spinlock lock;
char buf[INPUT_BUF];
uint r; // Read index
uint w; // Write index
uint e; // Edit index
} input;
I don't want to edit this file, but I do want to use input
in another file I made. I am not sure how to do this, external declarations like extern input
and extern struct input
do not work
Upvotes: 1
Views: 227
Reputation: 141544
There's no way to do this correctly without editing the file; two different untagged structs are considered to be different types.
It might "work" to repeat the struct definition in the other file, but that doesn't comply with the C standard.
Change this file to struct input_t {
etc. and make the other file do extern struct input_t {
... } input;
.
Of course it is preferable to put the struct definition (and any definitions it relies on such as spinlock
or INPUT_BUF
) in a common header file, this is to prevent ODR violations. If you don't do this then be very careful that both files are using the exact same struct definition. For example if INPUT_BUF
were different in one to the other, then it will cause undefined behaviour .
Upvotes: 4