How to solve issue with circular dependencies between 2 files in C

Viewed 37

I have a file called hittable.h that looks like this:

#ifndef HITTABLE_H
#define HITTABLE_H

#include "ray.h"
#include "vec3.h"
#include "material.h"
#include <stdbool.h>

typedef struct hit_record{
    point3 p;
    vec3 normal;
    material* mat_ptr;
    double t;
    bool front_face;

} hit_record;

static inline void set_face_normal(hit_record *rec, const ray *r, vec3 outward_normal) {
    rec->front_face = (dot(r->direction, outward_normal) < 0);
    rec->normal = rec->front_face ? outward_normal : negate(outward_normal);
}

#endif

and I have another file called material.h that looks like this:

#ifndef MATERIAL_H
#define MATERIAL_H
#include "vec3.h"
#include "ray.h"

typedef struct {
    color albedo;

} lambertian;

bool lambertian_scatter(lambertian lam, const ray* r_in, const hit_record* rec, color attenuation, ray* scattered) {
    return false;
}

typedef struct{
    int type;
    union {
        lambertian lam; 
    };
} material;


#endif

As you can see, in the the file material.h, the function lambertian_scatter()'s third argument is of type hit_record *. However, in hittable.h, the struct hit_record (which is required in material.h) has a member of type material which is defined in material.h. I tried several things and I couldn't fix this problem and was wondering what I should do.

0 Answers
Related