實(shí)驗(yàn)題目:? Linux下C程序的編寫(xiě)???????????????? ??????????
實(shí)驗(yàn)?zāi)康模?u>? (1)掌握Linux下C程序的編寫(xiě)、編譯與運(yùn)行方法;
(2)掌握gcc編譯器的編譯過(guò)程,熟悉編譯的各個(gè)階段;???????
(3)熟悉Makefile文件的編寫(xiě)格式和make編譯工具的使用方法。? ? ? ??
實(shí)驗(yàn)環(huán)境(硬件和軟件) VMware Workstation,Linux??????????????????????????????????
實(shí)驗(yàn)內(nèi)容:
練習(xí)使用gcc編譯器編譯C程序并執(zhí)行,編寫(xiě)Makefile文件,使用make工具編譯程序并執(zhí)行。具體內(nèi)容如下:
- 編寫(xiě)簡(jiǎn)單C程序,功能為在屏幕上輸出“Hello gcc!”。利用該程序練習(xí)使用gcc編譯器的E,S,c,o,g選項(xiàng),觀察不同階段所生成的文件,即*.c,*.i,*.s,*.o文件和可執(zhí)行文件。
- 編寫(xiě)一個(gè)由頭文件greeting.h、自定義函數(shù)greeting.c、主函數(shù)myapp.c構(gòu)成的C程序,并根據(jù)這三個(gè)文件的依賴(lài)關(guān)系編寫(xiě)Makefile文件。
實(shí)驗(yàn)步驟:
(1)1.創(chuàng)建空文檔,修改名稱(chēng)為myhello.c,出入程序代碼,保存并退出。
2.打開(kāi)終端,使用gcc命令對(duì)上述文檔分階段編譯。
3.利用ls命令查看編譯過(guò)程產(chǎn)生的各個(gè)文件。即myhello.c、myhello.s、myhello.o文件和可執(zhí)行文件myhello.c。
(2)Makefile文件的編寫(xiě)。使用make工具編譯程序,在終端提示符后輸入“make”,并按Enter。
實(shí)驗(yàn)數(shù)據(jù)記錄:
-
- vim創(chuàng)建空文檔myhello.c,輸入程序代碼,Esc后:wq保存并退出。
- vim myhello.c
-
/*----------myhello.c----------*/ #include <stdio.h> int main() { ??????? printf("Hello gcc!\n"); ? ? ? ? return 0; }
1.2使用gcc命令對(duì)上述文檔分階段編譯。
gcc -E myhello.c -o myhello.i? # 預(yù)處理階段
gcc -S myhello.i -o myhello.s? # 編譯階段
gcc -c myhello.s -o myhello.o? # 匯編階段
gcc myhello.o -o myhello?????? # 鏈接階段
還可以直接編譯gcc myhello.c -o myhello
3.利用ls命令查看編譯過(guò)程產(chǎn)生的各個(gè)文件。即myhello.c、myhello.s、myhello.o文件和可執(zhí)行文件myhello.c。
運(yùn)行結(jié)果:
(2)Makefile文件的編寫(xiě)。使用make工具編譯程序,在終端提示符后輸入“make”,并按Enter。
2.1myapp.c文件:? (vim myapp.c創(chuàng)建)
#include <stdio.h>
#include "greeting.h"
#define N 10
int main()
{
??????? char name[N];
??????? printf("Your name,please:");
??????? scanf("%s",name);
??????? greeting(name);
??????? return 0;
}
2.2 greeting.h
#ifndef _GREETING_H
#define _GREETING_H
void greeting(char *name);
#endif
2.3greeting.c
#include <stdio.h>
#include "greeting.h"
void greeting(char *name)
{
??????? printf("Hello %s",name);
}
2.4Makefile文件和make
vim Makefile創(chuàng)建并輸入代碼
myapp:greeting.o myapp.o
??????? gcc myapp.o greeting.o -o myapp
greeting.o:greeting.c greeting.h
??????? gcc -c greeting.c
myapp.o:myapp.c greeting.h
??????? gcc -c myapp.c
clean:
??????? rm -rf *.o
make編譯及最終結(jié)果:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-861227.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-861227.html
到了這里,關(guān)于Linux下C程序的編寫(xiě)(操作系統(tǒng)實(shí)驗(yàn))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!