導航:首頁 > 編程系統 > linuxcmakefile

linuxcmakefile

發布時間:2025-02-15 21:27:55

① [linux]編寫一個簡單的C語言程序,編寫Makefile文件

//calculate.h
#ifndef DEFCALCULATE_H
#define DEFCALCULATE_H
#include <string>
#include <map>
#include <iostream>
#include <cctype>
using namespace std;
// =========================================================================
// = 一些標志,如數字、+-*/()
enum Token_value
{
NAME,
NUMBER,
END,
PLUS='+',
MINUS='-',
MUL='*',
DIV='/',
PRINT=';',
ASSIGN='=',
LP='(',
RP=')'
};
//#############################################################################
//#############################################################################
double term(bool); //乘法
double prim(bool); //處理初等項
double error(const string&); //錯誤函數
Token_value get_token(); //輸入
double expr(bool get); //加和減
//#############################################################################
//#############################################################################
extern double number_value; //
extern string string_value; //
extern Token_value curr_tok; //當前操作標志
extern map<string,double> table;//
extern int no_of_errors; //
#endif

//winconsole.cpp
#include "calculate.h"
#include <sstream>
istream* input;
int main()
{
//switch(argc)
//{
//case 1:
input=&cin;
// break;
//case 2:
// input=new istringstream(argv[1]);
// break;
//default:
// error("too many arguments");
// return 1;
//}
table["pi"]=3.14159;
table["e"]=2.718182;
while (*input)
{
get_token();
if (curr_tok==END)
{
break;
}
if (curr_tok==PRINT)
{
continue;
}
cout<<expr(false)<<endl;
}
if (input!=&cin)
{
delete input;
}
return 0;
}
//error.cpp
#include "calculate.h"
int no_of_errors;
double error(const string& s)
{
no_of_errors++;
cerr<<"error:"<<s<<'\n';
return 1;
}
//expr.cpp
#include "calculate.h"
Token_value curr_tok=PRINT;
double expr(bool get)//加和減
{
double left=term(get);
for (;;)
{
switch(curr_tok)
{
case PLUS:
left+=term(true);
break;
case MINUS:
left-=term(true);
break;
default:
return left;
}
}
}
//get_token.cpp
#include "calculate.h"
extern Token_value curr_tok;
Token_value get_token()
{
char ch=0;
cin>>ch;
switch(ch)
{
case 0:
return curr_tok=END;
case ';':
case '*':
case '/':
case '+':
case '-':
case '(':
case ')':
case '=':
return curr_tok=Token_value(ch);
case '0':case '1':case '2':case '3':case '4':
case '5':case '6':case '7':case '8':case '9':
case '.':
cin.putback(ch);
cin>>number_value;
return curr_tok=NUMBER;
default:
if (isalpha(ch))
{
cin.putback(ch);
cin>>string_value;
return curr_tok=NAME;
}
error("bad token");
return curr_tok=PRINT;
}
}
//prim.cpp
#include "calculate.h"
double number_value;
string string_value;
map<string,double> table;
double prim(bool get)
{
if (get)
{
get_token();
}
switch(curr_tok)
{
case NUMBER:
{
double v=number_value;
get_token();
return v;
}
case NAME:
{
double& v=table[string_value];
if (get_token()==ASSIGN)
{
v=expr(true);
}
return v;
}
case MINUS:
{
return -prim(true);
}
case LP:
{
double e=expr(true);
if (curr_tok!=RP)
{
return error(")expected");
}
get_token();
return e;
}
default:
return error("primary expected");
}
}
//term.cpp
#include "calculate.h"
double term(bool get)
{
double left=prim(get);
for (;;)
{
switch(curr_tok)
{
case MUL:
left*=prim(true);
break;
case DIV:
if (double d=prim(true))
{
left/=d;
break;
}
return error("divide by 0");
default:
return left;
}
}
}
//makefile
objects = error.o expr.o get_token.o prim.o term.o winconsole.o
calculate:$(objects)
g++ -Wall -g -o calculate $(objects)
$(objects) : %.o : %.cpp
g++ -c $(CXXFLAGS) $< -o $@

//這是一個在linux下實現的簡單的計算器程序,實現帶括弧的+-*/運算的,由於復制進來代碼格式有點出入,你自己把代碼格式規范下,尤其是makefile文件里的命令前面一定要以一個tab開始

② linux編譯c文件makefilelinux編譯c文件

linux怎麼編譯c的源程序的?gcc,編譯命令是什麼?

編譯方法:格式gcc常用的選項最簡單的是:gcchello.c默認的情況下將生成a.out的可執行性文件,只需要在終端上輸入./a.out就可以看到執行的結果,如果你想指定生成目標文件的名字那麼你可以加上-o選項,命令如下:gcc-ohellohello.

c命令:gcc-chellohello.c(2)linuxcmakefile擴展閱讀:gcc命令的基本用法gcc其中,filenames為文件名;options為編譯選項。

當不使用任何編譯選項編譯hello.c時,gcc將會自動編譯產生一個a.out的可執行文件:#lshello.c#gcchello.c#lsa.outhello.c執行:#./a.outHello,World!使用-o編譯選擇,可以為編譯後的文件指定一個名字:#lsa.outhello.c#gcchello.c-ohello#lsa.outhellohello.c執行:#./helloHello,World!注意:使用-o選項時,-o後面必須跟一個文件名,即:-ooutfile。為了便於描述後面的選項,刪除hello和a.out可執行文件。

到底怎麼在Linux里編寫c程序啊?

gccfirst.C-ofirst說明:

1>編譯當前目錄下、名字叫做first.C的c源文件;

2>在當前目錄下、生成名字叫first(這個名字可以自己隨便寫、符合linux命名規則就行)的可執行程序;關於運行:用ls-l命令,可以看到當前目錄下有一個綠色的文件、名字叫first,就是剛剛編譯得到的文件;使用命令「./first」既可運行(無雙引號、雙引號是用來說明的);

怎麼在linux下用vim編寫一個C程序?

先終端輸入

vimtest.c

車進入vim編輯器再按a鍵進入編輯狀態輸入C語言語句:

#include

intmain(){

printf("helloworld!n");

}

按esc鍵退編輯狀態再輸入冒號(shift+冒號號鍵)緊跟著輸入wq即輸入

:wq

按車即推vim編輯器終端命令窗口(類似xp命令提示符)

輸入

gcctest.c步編譯

./a.out執行輸helloworld

結束前提已經安裝gcc

③ 在LINUX系統中編程序,makefile是怎麼生成的,是通過GCC或GDB編譯聯接生成的嗎

這位朋友我看是有點誤解了makefile的做用。makefile 是一個腳本,由他來控制編譯出的程序的版本。當專你改寫了原文件屬後,通過makefile 來檢查依賴關系,來生成最終的文件。比如一個程序叫a 他的源碼叫a.c

a.c 又用到頭文件a.h ,a.h是1.h和2.h生成的

如果你改寫了1.h,通過編寫makefile文件。make時會調用makefile來依此按依賴關系生成最後的a

所以說編譯時程序並不知道生成a都用到什麼。怎麼可能自己生成呢。

makefile是要你自己編寫的,來告訴make來如何編譯。make工具就相當於一個腳本。

閱讀全文

與linuxcmakefile相關的資料

熱點內容
電腦桌面文件夾美化 瀏覽:557
手機app沒刪怎麼找不到 瀏覽:263
美國國家網路安全局 瀏覽:362
騰訊雲主機root密碼是什麼意思 瀏覽:414
讓win10識別蘋果分區 瀏覽:935
如何在文件夾批量添加後綴 瀏覽:431
三星s5哪個版本最好 瀏覽:210
word段落開頭空格 瀏覽:162
如何開啟設置密碼 瀏覽:815
網路詞彙有什麼隱喻特徵 瀏覽:948
dede備案代碼 瀏覽:893
python抓取網頁文件夾 瀏覽:655
三菱系列怎麼編程 瀏覽:917
買茶幾在哪個網站 瀏覽:214
ecshop資料庫導入不了 瀏覽:632
cnc編程軟體一般用哪個好 瀏覽:239
js按價格排序 瀏覽:685
我要下載菜鳥app怎麼下呢 瀏覽:694
wordvba圖片提示 瀏覽:344
appstore無法下載視頻 瀏覽:209

友情鏈接