How to solve circular dependencies in C language

The system software includes two sub-modules mod_a and mod_b, which lead to circular dependencies due to the following reasons

  1. mod_a and mod_b need to use the public definition of sys, so they need to include sys_com.h
  2. sys_com.h provides a memory management interface. You need to know the memory node size (sizeof) of each module, so it contains mod_a.h and mod_b.h.

My personal idea is to create a new com.h, move the definitions of MOD_A_TYPE_1 and MOD_B_TYPE_1 to this file, and sys_com.h, mod_a.h and mod_b.h all include header files.
We often encounter similar problems during the project development process. What are your principles for dealing with them?Thanks in advance.

// mod_a.h

#include "sys_com.h"

typedef struct
{
    int len;
} MOD_A_TYPE_1;
// mod_b.h

#include "sys_com.h"

typedef struct
{
    int size;
} MOD_B_TYPE_1;
//sys_com.h

#include "mod_a.h"
#include "mod_b.h"

#define SYS_MOD_A_TYPE_1_SIZE           sizeof(MOD_A_TYPE_1)
#define SYS_MOD_B_TYPE_1_SIZE           sizeof(MOD_B_TYPE_1) 

// pool_type
#define SYS_MEM_POOL_MOD_A_TYPE_1       1
#define SYS_MEM_POOL_MOD_B_TYPE_1       2

// @pool_type: SYS_MEM_POOL_MOD_A_TYPE_1/SYS_MEM_POOL_MOD_B_TYPE_1
void* sys_alloc_pool_mem(int pool_type)

4

sys_com.h provides a memory management interface. You need to know the memory node size (sizeof) of each module, so it contains mod_a.h and mod_b.h.

Do you? Why? It seems to me that your API has a flag to choose the struct type but it does not need to use the size directly. The way I would do this is to put the #define for the sizes directly in the C file. In fact, I question the need for the #defines at all. What advantage do they give you over directly using sizeof(type)? I think sys_com.h should look like this:

// pool_type
#define SYS_MEM_POOL_MOD_A_TYPE_1       1
#define SYS_MEM_POOL_MOD_B_TYPE_1       2

// @pool_type: SYS_MEM_POOL_MOD_A_TYPE_1/SYS_MEM_POOL_MOD_B_TYPE_1
void* sys_alloc_pool_mem(int pool_type)

and sys_com.c should do the following

#include sys_com.h
#include "mod_a.h"
#include "mod_b.h"

// If you really must have the #defines
#define SYS_MOD_A_TYPE_1_SIZE           sizeof(MOD_A_TYPE_1)
#define SYS_MOD_B_TYPE_1_SIZE           sizeof(MOD_B_TYPE_1) 

void* sys_alloc_pool_mem(int pool_type)
{
// Implementation using the #defines or better sizeof(type)
}

Also consider putting include guards1 on all the headers to prevent circular includes e.g. in mod_a.h

#if !defined(_MOD_A_H)
#define _MOD_A_H

typedef struct
{
    int len;
} MOD_A_TYPE_1;

#endif

1 Many C compilers have #pragma once to do the same thing but it is not standard.

2

May be your can use an other file to declare your types.
So:

  • sys_com.h includes mod_types.h, not mod_a.h or mod_b.h
  • mod_a.h and mod_b.h includes sys_com.h

Does it fit to your case ?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật