การ Include header file ที่เราสร้างขึ้นมาโดยใช้ #include “file” แต่ว่าหากเราจะอ้างอิงที่อยู่ของ header file ในกรณีที่อยู่คนละ folder นั้นเราสามารถทำได้ โดยแบ่งได้เป็น 2 แบบ
แบบที่ 1 include header file ที่อยู่ใน folder ที่อยู่ใน level มากกว่า (folder ย่อยลึกเข้าไป)
จะใช้เครื่องหมายทับ \ ในการระบุ folder ย่อยๆเข้าไป โดยยึดจุดอ้างอิงจาก file .c ของเรา
สมมติว่าเรามี File ตามที่อยู่นี้
D:\Desktop\helloworld1.c
และ
D:\Desktop\test\helloworld1.h
เราสามารถ Include ได้โดย
#include <stdio.h> #include "test/helloworld1.h" int main (void) { printf("%s\n", HELLO); return 0; }
และแบบที่ 2 include header file ที่อยู่ใน folder ที่อยู่คนละ level
จะใช้เครื่องหมาย.จุดจุด .. ในการระบุ folder ที่ต้องออกมา 1 ชั้น โดยยึดจุดอ้างอิงจาก file .c ของเรา เช่นกัน
สมมติว่าเรามี File ตามที่อยู่นี้
D:\Desktop\main\helloworld2.c
และ
D:\Desktop\test\helloworld2.h
เราสามารถ Include ได้โดย
#include <stdio.h> #include "../test/helloworld2.h" int main (void) { printf("%s\n", HELLO); return 0; }
และอีกกรณี ก็คือ ต้องอ้างอิงออกไปมากกว่า 1 ชั้น โดย .. จะใช้แทนการอ้างอิงออกจาก folder นั้น 1 ชั้น
D:\Desktop\main\submain\helloworld3.c
และ
D:\Desktop\test\helloworld3.h
#include <stdio.h> #include "../../test/helloworld3.h" int main (void) { printf("%s\n", HELLO); return 0; }