基本認識:
#include <xxx>:首先去系統目錄中找頭文件,如果沒有在到當前目錄下找。像標准的頭文件 stdio.h、stdlib.h等用這個方法。
#include "xxx":首先在當前目錄下尋找,如果找不到,再到系統目錄中尋找。 這個用於include自定義的頭文件,讓系統優先使用當前目錄中定義的。
單個.c源文件:test.c
1 /*=====test.c=======*/ 2 #include <stdio.h> 3 4 int main(void) 5 { 6 printf("Hello, world!\n"); 7 return 0; 8 }
gcc -g test.c -o test
-g:為了GDB調試加入的參數;
./test
多個源文件: main.c hello.h hello.c
1 /*=====main.c=======*/ 2 #include <stdio.h> 3 4 #include "hello.h" 5 6 int main() 7 8 { 9 10 hello(); 11 12 return 0; 13 14 }
1 /*===hello.h=======*/
2 void hello();
1 /*====hello.c=======*/ 2 #include <stdio.h> 3 #include "hello.h" 4 void hello() 5 6 { 7 printf("Hello,world!.\n"); 8 9 }
gcc main.c hello.c -o main
./main