gcc(g 多檔案編譯

2021-06-25 14:51:43 字數 1164 閱讀 7193

1.簡單程式(單模組程式)的編譯

檔案file1.c

#include

int main()

檔案file1.cpp

#include

using std::cout;

using std::endl;

int main()

[xiaochen@freeware ~]$ gcc file1.c -o file1

[xiaochen@freeware ~]$ g++ file1.cpp -o file1_cpp

[xiaochen@freeware ~]$ ./file1 

hello

[xiaochen@freeware ~]$ ./file1_cpp

hello

對於只有乙個檔案的c/c++用gcc/g++來編譯很容易

對於多個檔案即多個模組的程式來說,其實也並不是很難.

2.多模組程式的編譯

下面舉個例子:

檔案first.h

int first();

檔案first.c

#include

#include "first.h"

first()

檔案second.h

int mymax(int,int);

檔案second.c

mymax(x,y)

檔案main.c

#include "first.h"

#include "second.h"

#include

int main()

下面是在終端中輸入的內容

[xiaochen@freeware ~]$ gcc -c first.c

[xiaochen@freeware ~]$ gcc -c second.c

[xiaochen@freeware ~]$ gcc -c main.c

[xiaochen@freeware ~]$ gcc first.o second.o main.o -o main

[xiaochen@freeware ~]$ ./main

this is just a test!20

當然啦也可以這麼輸入

[xiaochen@freeware ~]$ gcc first.c second.c main.c -o main

gcc g 編譯過程

階段輸入 輸出工具 示例預編譯 c i 預處理器cpp gcc e test.c o test.i 編譯 i s 彙編 編譯器egcs gcc s test.i o test.s 彙編 s o 目標 彙編器as gcc c test.s o test.o 鏈結 o 可執行檔案 聯結器ld gcc t...

多檔案編譯

編譯多個檔案 編輯兩個檔案 cs 和 class.cs cs using system namespace test class.cs using system namespace test public void saysomething 執行的命令 引數的使用都和單個檔案的執行一樣 如果不指定生...

多檔案編譯

一.常用編譯命令選項 假設源程式檔名為test.c。3.選項 e 用法 gcc e test.c o test.i 作用 將test.c預處理輸出test.i檔案。4.選項 s 用法 gcc s test.i 作用 將預處理輸出檔案test.i彙編成test.s檔案。5.選項 c 用法 gcc c ...