1

I'm writing a c program using curses library, and want to create some structs reflecting my application UI.

Here's my app.c file

#include <stdlib.h>
#include <stdio.h>
#include "screen.h"

int main() {
    struct screen scr = {
        .win1 = {
            .title = "win1"
        },
        .win2 = {
            .title = "win2"
        }
    };
}

here's screen.h

#ifndef SCREEN_H
#define SCREEN_H

#include "window.h"

struct screen {
    struct window win1;
    struct window win2;

    struct window *focused;
};

#endif

and here's window.h

#ifndef WINDOW_H
#define WINDOW_H

#include "screen.h"

struct window {
    char *title;
    void (*handle_key_event)(struct screen*);
};

#endif

My window struct handle method must receive a reference to screen, to be able to change the focused window in some specific cases. But when I compile this, I get the warning

window.h:8:34: warning: its scope is only this definition or declaration, which is probably not what you want

which is because it doesn't see the screen declaration. How to fix this?

1
  • If window.h only needs a screen pointer, you don't need to include the full definition there. You can just declare struct screen; there instead of including screen.h. I'd guess the error is that it thinks you're declaring a struct screen inside struct window, but I'm not sure exactly how it'd parse it like that. Commented Mar 25, 2019 at 14:38

2 Answers 2

4

The warning is when the first reference to a struct is inside something else.

Put struct screen; above the declaration of struct window.

Circular dependencies of header files is a bad idea. Consider refactoring. You do not need the definition of a struct to declare pointers to it. The forward declaration will suffice.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes. Much easier.
@Carmine: since all compilations would end up including both headers every time, it is simpler to combine the screen.h and window.h into one composite file. All else apart, it does take a little time (with the emphasis on little) to open each separate header, so compilations will be quicker with fewer headers. On tiny projects (under 10 files in total), that's immaterial — on projects with hundreds or thousands of source files, it can become measurable. If the two structures were not so inextricably intertwined, separation is good. But they're inextricably intertwined here.
2

This is about fundamental design, nothing else. To have two header files that mutually include each other simply doesn't make any sense, don't do this. #include is to be regarded as a one-way dependency. In program design, more complex objects depend on/consist of less complex ones.

For example a screen contains a window, so it should be the one doing #include "window.h". While a window doesn't know a thing about screens, it should only concern itself with displaying a window. Cross-communication between the two modules might have to be in a third module.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.