C แยกไฟล์และการคอมไพล์หลาย translation unit



โปรแกรมจริงไม่ได้อยู่ไฟล์เดียว — แยกเป็นโมดูล แต่ละโมดูลมีคู่ .h / .c

แนวคิด: interface กับ implementation

  • foo.h — *ประกาศ* ว่ามีฟังก์ชัน/type/ค่าคงที่อะไรบ้าง (interface ที่คนอื่นใช้)
  • foo.c — *นิยาม* โค้ดจริง (implementation ที่ซ่อนไว้)
  • ไฟล์ที่อยากใช้ foo เขียน #include "foo.h"

ตัวอย่าง: โมดูล mathutil

mathutil.h

#ifndef MATHUTIL_H
#define MATHUTIL_H

// ค่าคงที่ที่ใช้ร่วม
#define MATHUTIL_VERSION "1.0"

// ประกาศฟังก์ชัน (prototype เท่านั้น ไม่มี body)
int  gcd(int a, int b);
long factorial(int n);
int  is_prime(int n);

#endif

mathutil.c

#include "mathutil.h"      // include header ของตัวเอง — คอมไพเลอร์เช็คว่าตรงกัน

int gcd(int a, int b)
{
    while (b != 0) {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}

long factorial(int n)
{
    long result = 1;
    for (int i = 2; i <= n; i++) result *= i;
    return result;
}

int is_prime(int n)
{
    if (n < 2) return 0;
    for (int i = 2; i <= n / i; i++) {   // i <= n/i — เลี่ยง i*i overflow
        if (n % i == 0) return 0;
    }
    return 1;
}

factorial ข้างบนรองรับเฉพาะ n ที่ผลไม่เกิน LONG_MAX (long 64 บิต → n ถึง 20) และ gcd กับ INT_MIN, -1 มีปัญหาการหาร — API คณิตศาสตร์ควรระบุ domain ที่รองรับไว้เสมอ

main.c

#include <stdio.h>
#include "mathutil.h"

int main(void)
{
    printf("mathutil %s\n", MATHUTIL_VERSION);
    printf("gcd(12, 18) = %d\n", gcd(12, 18));
    printf("5! = %ld\n", factorial(5));
    printf("17 เป็นจำนวนเฉพาะ: %d\n", is_prime(17));
    return 0;
}

คอมไพล์

วิธีเดียว: ให้ทุกไฟล์ในบรรทัดเดียว

gcc -std=c17 -Wall -Wextra main.c mathutil.c -o prog

วิธีที่ดีกว่า: คอมไพล์แยกเป็น object file แล้ว link

gcc -std=c17 -Wall -Wextra -c mathutil.c   # -> mathutil.o
gcc -std=c17 -Wall -Wextra -c main.c       # -> main.o
gcc main.o mathutil.o -o prog              # link

ข้อดีของวิธีแยก: แก้แค่ main.c → คอมไพล์ใหม่แค่ main.o แล้ว link (ไม่ต้องคอมไพล์ mathutil.c ใหม่) — บทที่ 27 (Make) จะจัดการเรื่องนี้ให้อัตโนมัติ

translation unit

หนึ่ง .c + header ที่มัน include ทั้งหมด = หนึ่ง translation unit คอมไพเลอร์ประมวลผลทีละ unit แยกกันเป็น .o แต่ละ unit ไม่เห็นกัน ตอนคอมไพล์ — มารู้จักกันตอน link

นี่คือเหตุผลที่ต้องมี prototype ใน .h: main.c รู้แค่ว่า gcd มีลายเซ็นอะไร (จาก header) แต่ไม่เห็นโค้ด — จนกว่า linker จะจับ main.o กับ mathutil.o มาต่อกัน

undefined reference to 'gcd'   ← linker หา symbol ไม่เจอ (ลืม mathutil.o?)

static ระดับไฟล์ — ซ่อนจากโมดูลอื่น

ฟังก์ชัน/ตัวแปรที่เป็น “รายละเอียดภายใน” ของโมดูล ใส่ static ทำให้มองไม่เห็นจากไฟล์อื่น:

// mathutil.c

static int square(int x)        // ตัวช่วยภายใน — ไฟล์อื่นเรียกไม่ได้
{
    return x * x;
}

int sum_of_squares(int n)       // ฟังก์ชัน public (มีใน .h)
{
    int total = 0;
    for (int i = 1; i <= n; i++) total += square(i);
    return total;
}

เหมือน private ในภาษาอื่น — ไม่ประกาศใน .h + ใส่ static = ภายในโมดูลเท่านั้น หลายทีมใส่ static กับทุกอย่างที่ไม่ต้อง export

ตัวแปร global ข้ามไฟล์ — extern

หลีกเลี่ยงถ้าทำได้ แต่ถ้าจำเป็น:

config.h

#ifndef CONFIG_H
#define CONFIG_H
extern int g_verbose;       // ประกาศ (มีตัวแปรนี้อยู่ที่ไหนสักที่)
#endif

config.c

#include "config.h"
int g_verbose = 0;          // นิยาม (จองพื้นที่จริง — ครั้งเดียวทั้งโปรแกรม)

main.c

#include "config.h"
// ใช้ g_verbose ได้

extern int x; = “ประกาศ” (บอกว่ามี), int x; = “นิยาม” (จองพื้นที่) — นิยามได้ ครั้งเดียว ทั้งโปรแกรม

กฎการนิยามครั้งเดียว

(C++ เรียกกฎนี้ว่า ODR — one definition rule; C ไม่มีชื่อเรียกเป็นทางการ แต่หลักคล้ายกัน)

  • ประกาศ (declaration) ซ้ำได้ — prototype, extern, typedef, struct ใน header
  • นิยาม (definition) ได้ครั้งเดียว — body ของฟังก์ชัน, ตัวแปร global ที่มีค่าเริ่มต้น

ผิดกฎ → multiple definition of 'foo' ตอน link

อย่านิยามฟังก์ชันหรือตัวแปร (ไม่ใช่ extern) ใน .h — ถ้า header ถูก include หลายไฟล์จะนิยามซ้ำ (ข้อยกเว้นในทางปฏิบัติคือ static inline function)

ต่างจาก C++: ใน C ตัวแปร const int x = 1; ที่ระดับไฟล์ยังมี external linkage — ใส่ใน header ที่ถูก include หลาย .c ก็ยัง multiple definition ต้องใช้ extern const ใน header + นิยามครั้งเดียวใน .c หรือ static const ถ้ายอมให้แต่ละ translation unit มีสำเนาของตัวเอง

int x; ที่ระดับไฟล์ (ไม่มีค่าเริ่มต้น ไม่มี extern) เป็น *tentative definition* — ในไฟล์เดียวเขียนซ้ำได้ แต่ใน header ที่หลาย .c include จะกลายเป็นหลายนิยาม (คอมไพเลอร์ใหม่ ๆ / -fno-common / C23 จะฟ้อง error)

สิ่งที่ควร / ไม่ควรใส่ใน .h

ใส่ใน .h ได้อย่าใส่ใน .h
prototype ของฟังก์ชันbody ของฟังก์ชัน (ยกเว้น static inline)
typedef, struct / enum / unionนิยามตัวแปร global (ใช้ extern)
#define มาโครโค้ดที่ทำงาน
extern declaration / extern constนิยาม const ตรง ๆ (external linkage ใน C)

เรื่องที่คนมักพลาด

  • ลืมใส่ .c ทุกไฟล์ตอนคอมไพล์/link — undefined reference to 'foo'
  • .h ไม่มี include guard — นิยาม struct/typedef ซ้ำเมื่อ include ทางอ้อม
  • นิยามฟังก์ชันใน .h — multiple definition เมื่อหลาย .c include
  • .h ไม่ตรงกับ .c — ลายเซ็นต่างกัน = UB (คอมไพเลอร์ไม่จับเพราะคนละ TU) — .c ควร #include header ของตัวเอง
  • ตัวแปร global ใน .h แบบ int x; (ไม่มี extern) — นิยามซ้ำทุกไฟล์
  • ลืม static กับฟังก์ชันตัวช่วย — ชื่อชนกับโมดูลอื่น (multiple definition หรือเรียกผิดตัว)
  • #include "file.c" — อย่า include ไฟล์ .c include เฉพาะ .h

สรุป

  • แยกโมดูลเป็น foo.h (prototype/type/มาโคร — มี guard) + foo.c (โค้ดจริง — #include "foo.h")
  • ผู้ใช้: #include "foo.h" แล้วคอมไพล์/link foo.c ไปด้วย
  • แต่ละ .c = translation unit คอมไพล์แยก (-c → .o) แล้ว linker ต่อกัน
  • static ที่ระดับไฟล์ = private ต่อโมดูล (ตัวช่วยภายใน)
  • ตัวแปร global ข้ามไฟล์: extern ใน .h, นิยามจริงครั้งเดียวใน .c หนึ่งตัว
  • กฎนิยามครั้งเดียว: ประกาศซ้ำได้ นิยามครั้งเดียว — อย่านิยามฟังก์ชัน/ตัวแปรใน .h (const ที่ระดับไฟล์ใน C มี external linkage — ต้อง extern const)

บทถัดไปว่าด้วย Makefile — จัดการการคอมไพล์หลายไฟล์แบบ incremental โดยไม่ต้องพิมพ์ gcc เอง