導航:首頁 > 操作系統 > linuxc系統編程

linuxc系統編程

發布時間:2022-09-23 21:51:26

linux系統怎麼c語言編程

比如編寫一個C語言文件
1.cpp
【建議使用gedit,可以使用中文哦】
然後簡單方法就是:
g++
1.cpp
然後./a.out
稍微麻煩點就是
g++
1.cpp
-o
out
./out

Ⅱ linux系統怎麼c語言編程

比如編寫一個C語言文件 1.cpp 【建議使用gedit,可以使用中文哦】
然後簡單方法就是:
g++ 1.cpp
然後./a.out

稍微麻煩點就是
g++ 1.cpp -o out
./out

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

在linux下通常使用gedit或vim直接編寫.c程序,然後通過gcc指令編譯。以Ubuntu系統為例,詳細過程如下:

1、進入桌面Temp文件夾

Ⅳ 怎麼用linux寫c語言

Linux正在成為開發人員的編程天堂,成為開源和免費操作系統。 Turbo C編譯器已經是一種編譯程序的舊方法,所以讓程序員轉向Linux以獲得新的編程環境。 在本文中,我們將解釋如何編寫,編譯和運行一個簡單的C程序。 這將成為您遷移到可以在Linux上編寫和執行的更復雜和有用的C程序的基礎。

我們在Ubuntu 18.04 LTS系統上運行了本文中提到的步驟和命令

我們將使用Linux命令行工具Terminal,以編譯一個簡單的C程序。 要打開終端,您可以使用Ubuntu Dash或Ctrl + Alt + T快捷方式。

第1步:安裝build-essential軟體包

為了編譯和執行C程序,您需要在系統上安裝必要的軟體包。 在Linux終端中以root用戶身份輸入以下命令:

sudo apt-get install build-essential

系統會要求您輸入root用戶密碼; 安裝過程將在此之後開始。 請確保您已連接到互聯網。

第2步:編寫一個簡單的C程序

安裝必要的軟體包之後,讓我們編寫一個簡單的C程序。

打開Ubuntu的圖形文本編輯器,將以下示常式序寫入或復制到其中:

#include<stdio.h>

int main()

{

printf("nA sample C program www.linuxidc.comnn");

return 0;

}

然後使用.c擴展名保存文件。 在這個例子中,我將我的C程序命名為linuxidc.c

或者,您可以通過gedit中的終端編寫C程序,如下所示:

gedit linuxidc.c

這將創建一個.c文件,您可以在其中編寫和保存程序。

第3步:使用gcc編譯C程序

在終端中,輸入以下命令以生成您編寫的程序的可執行版本:

句法:

$ gcc [programName].c -o programName

示例:

$ gcc linuxidc.c -o linuxidc

Ⅳ 介紹一下LINUX環境下C的編寫

先安裝GCC編譯器
1. 下載

在GCC網站上(http://gcc.gnu.org/)或者通過網上搜索可以查找到下載資源。目前GCC的最新版本為 3.4.0。可供下載的文件一般有兩種形式:gcc-3.4.0.tar.gz和gcc-3.4.0.tar.bz2,只是壓縮格式不一樣,內容完全一致,下載其中一種即可。

2. 解壓

根據壓縮格式,選擇下面相應的一種方式解包(以下的「%」表示命令行提示符):

% tar xzvf gcc-3.4.0.tar.gz
或者
% bzcat gcc-3.4.0.tar.bz2 | tar xvf -

新生成的gcc-3.4.0這個目錄被稱為源目錄,用${srcdir}表示它。以後在出現${srcdir}的地方,應該用真實的路徑來替換它。用pwd命令可以查看當前路徑。

在${srcdir}/INSTALL目錄下有詳細的GCC安裝說明,可用瀏覽器打開index.html閱讀。

3. 建立目標目錄

目標目錄(用${objdir}表示)是用來存放編譯結果的地方。GCC建議編譯後的文件不要放在源目錄${srcdir]中(雖然這樣做也可以),最好單獨存放在另外一個目錄中,而且不能是${srcdir}的子目錄。

例如,可以這樣建立一個叫 gcc-build 的目標目錄(與源目錄${srcdir}是同級目錄):

% mkdir gcc-build
% cd gcc-build

以下的操作主要是在目標目錄 ${objdir} 下進行。

4. 配置

配置的目的是決定將GCC編譯器安裝到什麼地方(${destdir}),支持什麼語言以及指定其它一些選項等。其中,${destdir}不能與${objdir}或${srcdir}目錄相同。

配置是通過執行${srcdir}下的configure來完成的。其命令格式為(記得用你的真實路徑替換${destdir}):

% ${srcdir}/configure --prefix=${destdir} [其它選項]

例如,如果想將GCC 3.4.0安裝到/usr/local/gcc-3.4.0目錄下,則${destdir}就表示這個路徑。

在我的機器上,我是這樣配置的:

% ../gcc-3.4.0/configure --prefix=/usr/local/gcc-3.4.0 --enable-threads=posix --disable-checking --enable--long-long --host=i386-redhat-linux --with-system-zlib --enable-languages=c,c++,java

將GCC安裝在/usr/local/gcc-3.4.0目錄下,支持C/C++和JAVA語言,其它選項參見GCC提供的幫助說明。

5. 編譯

% make

這是一個漫長的過程。在我的機器上(P4-1.6),這個過程用了50多分鍾。

6. 安裝

執行下面的命令將編譯好的庫文件等拷貝到${destdir}目錄中(根據你設定的路徑,可能需要管理員的許可權):

% make install

至此,GCC 3.4.0安裝過程就完成了。

6. 其它設置

GCC 3.4.0的所有文件,包括命令文件(如gcc、g++)、庫文件等都在${destdir}目錄下分別存放,如命令文件放在bin目錄下、庫文件在lib下、頭文件在include下等。由於命令文件和庫文件所在的目錄還沒有包含在相應的搜索路徑內,所以必須要作適當的設置之後編譯器才能順利地找到並使用它們。

6.1 gcc、g++、gcj的設置

要想使用GCC 3.4.0的gcc等命令,簡單的方法就是把它的路徑${destdir}/bin放在環境變數PATH中。我不用這種方式,而是用符號連接的方式實現,這樣做的好處是我仍然可以使用系統上原來的舊版本的GCC編譯器。

首先,查看原來的gcc所在的路徑:

% which gcc

在我的系統上,上述命令顯示:/usr/bin/gcc。因此,原來的gcc命令在/usr/bin目錄下。我們可以把GCC 3.4.0中的gcc、g++、gcj等命令在/usr/bin目錄下分別做一個符號連接:

% cd /usr/bin
% ln -s ${destdir}/bin/gcc gcc34
% ln -s ${destdir}/bin/g++ g++34
% ln -s ${destdir}/bin/gcj gcj34

這樣,就可以分別使用gcc34、g++34、gcj34來調用GCC 3.4.0的gcc、g++、gcj完成對C、C++、JAVA程序的編譯了。同時,仍然能夠使用舊版本的GCC編譯器中的gcc、g++等命令。

6.2 庫路徑的設置

將${destdir}/lib路徑添加到環境變數LD_LIBRARY_PATH中,最好添加到系統的配置文件中,這樣就不必要每次都設置這個環境變數了。

例如,如果GCC 3.4.0安裝在/usr/local/gcc-3.4.0目錄下,在RH Linux下可以直接在命令行上執行或者在文件/etc/profile中添加下面一句:

setenv LD_LIBRARY_PATH /usr/local/gcc-3.4.0/lib:$LD_LIBRARY_PATH

7. 測試

用新的編譯命令(gcc34、g++34等)編譯你以前的C、C++程序,檢驗新安裝的GCC編譯器是否能正常工作。

8. 根據需要,可以刪除或者保留${srcdir}和${objdir}目錄。

如果用的是ubuntu或者是fedora的話 可以在源里直接安裝

用自帶的文本編譯器寫好文件後 打開「終端」輸入Gcc 就好了

Ⅵ 在Linux下如何開發C程序

在Linux開發環境下,GCC是進行C程序開發不可缺少的編譯工具。GCC是GNU C Compile的縮寫,是GNU/Linux系統下的標准C編譯器。雖然GCC沒有集成的開發環境,但堪稱是目前效率很高的C/C++編譯器。《linux就該這么學》非常值得您一看。Linux平台下C程序開發步驟如下:
1.利用編輯器把程序的源代碼編寫到一個文本文件中。
比如編輯test.c程序內容如下:
/*這是一個測試程序*/
#include<stdio.h>
int main(void)
{
printf("Hello Linux!");
}
2.用C編譯器GCC編譯連接,生成可執行文件。
$gcc test.c
編譯完成後,GCC會創建一個名為a.out的文件。如果想要指定輸出文件,可以使用選項-o,命令如下所示:
$gcc-o test1 test.c
這時可執行文件名就變為test1,而不是a.out。
3.用C調試器調試程序。
4.運行該可執行文件。 在此例中運行的文件是:
$./a.out 或者 test1
結果將得出:
Hello Linux!
除了編譯器外,Linux還提供了調試工具GDB和程序自動維護工具Make等支持C語言編程的輔助工具。如果想要了解GCC的所有使用說明,使用以下命令:
$man gcc

Ⅶ linux c編程

/*
Name: list.c
Author: guozan _SCS_BUPT
Mail: [email protected]
Date: 2010/4/6
實驗目的:練習vi,使用UNIX的系統調用和庫函數,體會UNIX文件通配符的處理方式以及命令對選項的處理方式。
編程實現程序list.c,列表普通磁碟文件(不考慮目錄和設備文件等),列出文件名和文件大小。
與ls命令類似,命令行參數可以有0到多個
0個參數:列出當前目錄下所有文件
參數為普通文件:列出文件
參數為目錄:列出目錄下所有文件
實現自定義選項r,a,l,h,m以及--
r 遞歸方式列出子目錄
a 列出文件名第一個字元為圓點的普通文件(默認情況下不列出文件名首字元為圓點的文件)
l 後跟一整數,限定文件大小的最小值(位元組)
h 後跟一整數,限定文件大小的最大值(位元組)
m 後跟一整數n,限定文件的最近修改時間必須在n天內
-- 顯式地終止命令選項分析
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
/*
slective options about ls
rflag is about recursive
aflag is about ones with . infront
lflag is about the minimum size
hflag is about the maximum size
mflag is about the modified time
*/
int rflag, aflag, lflag, hflag, mflag;
long modified_time; //the last time file be modified, days ago
off_t lower_size; //file's minimum size
off_t upper_size; //file's maximum size
/*
set the flags, thus the ls option
*/
void getoptions(int argc, char *argv[])
{
char ch;
//clear, all unseted
rflag = 0; aflag = 0; lflag = 0; hflag = 0; mflag = 0;
//use getopt to get the options, want to know more, call man
//the last one or after -- was set in argv[optind]
while ((ch = getopt(argc, argv, "ral:h:m:")) != -1) {
switch (ch) {
case 'r': rflag = 1; break;
case 'a': aflag = 1; break;
case 'l': lflag = 1; lower_size = atol(optarg); break;
case 'h': hflag = 1; upper_size = atol(optarg); break;
case 'm': mflag = 1; modified_time = atol(optarg); break; //get days
case '?': printf("Unknown option: %c\n", (char)optopt); break;
default : printf("Step into default\n"); break;
}
}
}
/*
the function to list things in path
*/
int ls(char *path)
{
struct stat st; //for check this is a directory or file
char temp[100]; //if path is null, it is used to get current directory
// get the path
if (path == NULL || path[0] == '-') {
path = temp;
getcwd(path, 100);
}
/* open the inode of file */
if (lstat(path, &st)) {
fprintf(stderr, "Error: %s not exist.\n", path);
return (-1);
}
/* judge whether the file is a file or a directory */
if (S_ISDIR(st.st_mode)) {
ls_dir(path);
}
else if (S_ISREG(st.st_mode)) {
print(path);
}
else {
printf("Not ordinary file, wouldn't be listed.\n");
}
return 0;
}
/*
list dirs, may recursively or not, depending on rflag
one thing is sure that it will list directories and files first,
then consider the things in the directories
*/
int ls_dir(char *path)
{
DIR *dp = NULL;
struct dirent *dirp = NULL;
if (path[0] != '.' || (path[0] == '.' && aflag == 1)) {
printf("\n%s:\n****************************************\n", path);
/* open the directory */
if ((dp = opendir(path)) == NULL) {
fprintf(stderr, "Error: can't open directory %s!\n", path);
return (-1);
}
chdir(path);
/* list all the things in directory */
while ((dirp = readdir(dp)) != NULL) {
print(dirp->d_name);
}
/* recursively ls dirs, after ls things together,
it's time to list things in children directory */
if (rflag == 1) {
rewinddir(dp); //reset dp
while ((dirp = readdir(dp)) != NULL) {
if (strcmp(dirp->d_name, ".") == 0
|| strcmp(dirp->d_name, "..") == 0) { //no current and parent directory
continue;
}
ls_dir_r(dirp->d_name); //only list directories, judged inside the function
}
}
/* close the directory */
if (closedir(dp)) {
fprintf(stderr, "Error: can't close the directory %s!\n", path);
return -1;
}
chdir("..");
}
return 0;
}
/*
list directories recursively,
only directories, nomatter what path you put in
*/
int ls_dir_r(char *path)
{
struct stat st;
/* open the inode of file */
if (lstat(path, &st)) {
fprintf(stderr, "Error: %s not exist.\n", path);
return (-1);
}
/* only ls directories */
if (S_ISDIR(st.st_mode)) {
ls_dir(path);
}
}
/*
print the filetype/size/name on the screen
*/
int print(char *path)
{
struct stat st;
time_t tp;
char *filename = NULL;
//get current time
time(&tp);
if (lstat(path, &st)) {
fprintf(stderr, "Error: %s can't be opened.\n", path);
return (-1);
}
/* get file name */
if ((filename = strrchr(path, '/')) != NULL) {
filename++;
}
else {
filename = path;
}
/* judge whether to list the file */
if ((S_ISDIR(st.st_mode)|| S_ISREG(st.st_mode)) //only directories and normal files
&& (lflag == 0 || (lflag == 1 && (st.st_size >= lower_size))) //the min size
&& (hflag == 0 || (hflag == 1 && (st.st_size <= upper_size))) //the max size
&& (mflag == 0 || (mflag == 1 && ((tp - st.st_mtime) <= modified_time * 24 * 60 * 60))) //modified time
&& (aflag == 1 || (aflag == 0 && filename[0] != '.')) //file with a '.' infront
) {
printf("%s\t%10ld\t%s\n", (S_ISDIR(st.st_mode) ? "DIR": "FILE"), st.st_size, filename);
}
return 0;
}
/*
The main function
*/
int main(int argc, char *argv[])
{
getoptions(argc, argv);
ls(argv[optind]);
return 0;
}

Ⅷ linux c編程

變數定義的時候出錯了,RECORDrec; RECORD和rec中間要加一個空格,

從錯誤信息里你也可以很容易找到原因的,跟錯錯誤提示,很容易定位到問題的地方,

不要被英文嚇到,其實沒什麼的

Ⅸ linux系統怎麼寫c語言

可以用gedit命令或者vim命令創建一個.c,然後編寫代碼,最後GCC編譯,或者在WIN上寫好,然後放在Linux裡面編譯,一定要用Linux編譯,才能在Linux終端跑該程序

Ⅹ Linux下的c編程:系統調用

標準的c函數庫是所有的編譯都要具有的函數庫,(實際上還是略有不同),但是這些基本上實現方法略有不同,但是結果和標準是一樣的。但是linux的系統調用,調用是linux的系統庫,比如說unistd.h下的fork這個是Linux下特有,你在vs上,就沒有這個庫,也沒有這個函數。同樣在vs上寫c,你可以引入頭文件比如windows.h,顯然這個庫是Linux不具有的。簡單說系統調用庫根據具體的操作系統環境不同而不同,而c標准庫,是所有支持c語言編譯器都有的。

閱讀全文

與linuxc系統編程相關的資料

熱點內容
部隊抗洪搶險命令範文 瀏覽:884
歐姆龍plc編程軟體使用教程 瀏覽:590
ai文件pdf 瀏覽:911
騰訊雲伺服器掛載混合雲 瀏覽:758
智能小車用什麼單片機 瀏覽:463
java怎麼給窗口關閉 瀏覽:940
列舉51單片機的定址方式 瀏覽:706
剪輯app怎麼寫長篇文字 瀏覽:400
app專屬流量過月租怎麼不更新 瀏覽:654
王者程序員都有誰 瀏覽:76
給牛換腳掌解壓 瀏覽:387
圍棋有多少種演算法 瀏覽:602
unity資源包在哪個文件夾 瀏覽:704
阿里雲伺服器遠程鏈接不成功 瀏覽:482
文件系統pdf 瀏覽:766
原神安卓區服什麼意思 瀏覽:37
貝殼app怎麼線上發布 瀏覽:159
如何挑選安卓系統機頂盒 瀏覽:54
安卓快充使用有什麼注意事項 瀏覽:909
黑馬程序員的雲計算網課 瀏覽:948