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);
#endifmathutil.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; // ประกาศ (มีตัวแปรนี้อยู่ที่ไหนสักที่)
#endifconfig.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 ที่หลาย.cinclude จะกลายเป็นหลายนิยาม (คอมไพเลอร์ใหม่ ๆ /-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เมื่อหลาย.cinclude .hไม่ตรงกับ.c— ลายเซ็นต่างกัน = UB (คอมไพเลอร์ไม่จับเพราะคนละ TU) —.cควร#includeheader ของตัวเอง- ตัวแปร global ใน
.hแบบint x;(ไม่มีextern) — นิยามซ้ำทุกไฟล์ - ลืม
staticกับฟังก์ชันตัวช่วย — ชื่อชนกับโมดูลอื่น (multiple definitionหรือเรียกผิดตัว) #include "file.c"— อย่า include ไฟล์.cinclude เฉพาะ.h
สรุป
- แยกโมดูลเป็น
foo.h(prototype/type/มาโคร — มี guard) +foo.c(โค้ดจริง —#include "foo.h") - ผู้ใช้:
#include "foo.h"แล้วคอมไพล์/linkfoo.cไปด้วย - แต่ละ
.c= translation unit คอมไพล์แยก (-c→.o) แล้ว linker ต่อกัน staticที่ระดับไฟล์ = private ต่อโมดูล (ตัวช่วยภายใน)- ตัวแปร global ข้ามไฟล์:
externใน.h, นิยามจริงครั้งเดียวใน.cหนึ่งตัว - กฎนิยามครั้งเดียว: ประกาศซ้ำได้ นิยามครั้งเดียว — อย่านิยามฟังก์ชัน/ตัวแปรใน
.h(constที่ระดับไฟล์ใน C มี external linkage — ต้องextern const)
บทถัดไปว่าด้วย Makefile — จัดการการคอมไพล์หลายไฟล์แบบ incremental โดยไม่ต้องพิมพ์ gcc เอง