比如编写一个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语言编译器都有的。