Linux 下編譯具有多個檔案的C 程式

2021-09-26 20:43:57 字數 665 閱讀 7116

第一次寫文章,本人是小白,主要是為了記錄一下學習過程,大神請左上角退出。

以簡單的hello world程式為例。

首先在工作目錄下建立func.**件和func.cpp檔案。在shell中輸入

vi func.h
建立func.**件。在func.h中輸入

void greet();
在vi中使用:wq命令儲存。

建立func.cpp檔案:

vi func.cpp
在func.cpp中輸入

#include "func.h"

#include using std::cout;

using std::endl;

void greet()

g++

*.cpp -o test

其中g++表示使用c++編譯器,*.cpp表示對當前資料夾中所有的.cpp檔案進行編譯,-o後面的名稱表示生成的可執行檔案的名稱。

最後使用下面的命令執行生成的可執行檔案test,將在shell中列印出"hello world!"

.

/test

linux下編譯多個檔案

例如 home files資料夾下有檔案main.c home files common資料夾下有檔案tou.h和shixian.c。tou.h為 若有多個shixian檔案,比如shixian1.c,shixian2.c,都可以在tou.h中用extern宣告出來。shixian.c為 編譯時用命...

編譯多個檔案

假設我們寫了乙個foo.h的標頭檔案,如下 ifndef foo h define foo h namespace m1 endif include include foo.h 這裡一定要打雙引號,因為它不是標準標頭檔案。void m1 foo 最後,我們在寫乙個main.c檔案來呼叫它 inclu...

Linux下多檔案的編譯(2)

myhello.c include include test.h include abc.h void print void int main void print void abc.c include abc.h void abc void abc.h include void abc void ...