1. 程式人生 > >linux GCC 編譯多個.c/.h檔案

linux GCC 編譯多個.c/.h檔案

基本認識:

#include <xxx>:首先去系統目錄中找標頭檔案,如果沒有在到當前目錄下找。像標準的標頭檔案 stdio.h、stdlib.h等用這個方法。 

#include "xxx":首先在當前目錄下尋找,如果找不到,再到系統目錄中尋找。 這個用於include自定義的標頭檔案,讓系統優先使用當前目錄中定義的。

單個.c原始檔:test.c

/*=====test.c=======*/
#include <stdio.h>  
   
int main(void)  
{  
        printf("Hello, world!\n");  
        return 0;  
}

編譯方式:

gcc -g test.c  -o test

-g:為了GDB除錯加入的引數;

./test

多個原始檔: main.c    hello.h    hello.c

/*=====main.c=======*/
#include <stdio.h>  

#include "hello.h"  

int main()  

{  

        hello();  

        return 0;  

}
/*===hello.h=======*/

void hello();
/*====hello.c=======*/
#include <stdio.h>  
#include "hello.h"  
void hello()  

{  
        printf("Hello,world!.\n");  

}
編譯方式:

gcc main.c hello.c -o main

./main