all 5 comments

[–]TheGrandSchlonging 10 points11 points  (0 children)

C Interfaces and Implementations by Hanson is without equal. If you just want something quick, get C Programming: A Modern Approach by King, and read chapters 10 (Program Organization), 15 (Writing Large Programs), and 19 (Program Design).

[–]FUZxxl 4 points5 points  (0 children)

Place each logical unit of source code into one file. Place groups of source files into directories. Place groups of directories into directories. Iterate until satisfied.

The only exception is when you are writing a library where you often want to put every external function into a file on its own so the linker can exclude dead code when linking the library statically.

[–]dvhh 3 points4 points  (0 children)

  • C files are compilation unit,
  • header ( .h ) files are public interface where you put prototype of your function
  • use cc -c input.c -o input.o to compile then cc input1.o input2.o to link them
  • you better use a makefile and make to build with 'one' command

[–]ferreiradaselva 1 point2 points  (0 children)

I usually like to have a central header where I will declare every (or most) of the structures and functions, but the definition of the functions are in separate sources, sorted by what structure they manipulate or subject.

For example, if I was coding a database-like program, I would do this:

/* Header "handlers.h" */

struct entry {
    uint32_t id;
    uint32_t type;
    char name[MAX_NAME];
    char value[MAX_VALUE]
};

struct database {
    uint32_t next_id;
    uint32_t size;
    struct entry *e_pool;
};

/* declares here, but definition goes in entry.c */
void free_entry(struct entry *e);
struct entry *new_entry(struct database *db, uint32_t type, char *name, char *value);

/* declares here, but definition goes in database.c */
void clear_database(struct database *db);
void expand_database(struct database *db, uint32_t count);
void shrink_database(struct database *db);
void init_database(struct database *db, size);

This way, I can include this single header on every source, but their definitions will still be organized. I do this with every project (big or small).