Linux下C程序的编辑、编译和运行

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/AdminGuan/article/details/90170641

1、通过命令 touch main.c 直接创建该文件;通过命令 ls 查看创建是否成功。

2、创建完main.c文件后,进行编写文件,ubuntu下提供了一个程序编辑软件gedit。借助命令 gedit main.c 就会弹出下图所示的编辑对话框,我们就可以在其中进行编写代码了。

 3、编写完代码后ctrl+s保存并退出编辑对话框即可。

4、接下来需要进行编译,在命令行中输入命令 gcc main.c ,如下图所示,如果没有额外的错误或者警告信息打印出来,说明编译成功

 

5、通过 ls 命令查看,会发现 gcc main.c 编译完成后,会出现一个a.out文件,这个是编译main.c文件后生成的一个可执行文件。

6、我们通过命令 ./a.out 运行该文件,就会运行我们编写的程序了。

附顺序查找代码:

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int ordersearch(int a[], int n, int des){
    int i;
    for(i=0; i<n; i++)
        if(des==a[i])
            return 1;
    return 0;
}
int main(){
    int i, val;
    int a[8] = {32,12,56,78,76,45,43,98};
    int ret;
    for(i=0; i<8; i++)
        printf("%d\t", a[i]);
    printf("\n请输入所要查找的元素:");
    while(1){
        scanf("%d", &val);
        fflush(stdin);
        ret = ordersearch(a, 8, val);
        if(1 == ret)
            printf ("查找成功!");
        else
            printf ("查找失败!");
        printf("\n请输入所要查找的元素:");
    }
    return 0;
}

或者:

#include <stdio.h>
void main() {
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int i,x,y;
printf("输入你要查找的数:\n");
scanf("%d", & x);
y = 0; //标记是否已找到,y=1表是找到了,y=0表示没找到
for (i = 0; i < 10; i++) //循环,把x和数组中的元素一个个比较
{
    if (x == a[i]) //如果x=a[i]说明已经找到
    {
        y = 1; //把y变成1,说明已经找到了
        printf("你要查找的数%d在第个%d位置\n", x, i + 1); //输出找到的相关信息
        break; //跳出循环
    }
}
if (y == 0) printf("无法找到你要查找的数\n"); //y=0表示找不到
}

 

展开阅读全文

linux编译运行c程序出现问题

03-12

大家好,rn我在linux下尝试编译和运行c程序, 这个程序中用到了zlib 函数库, 其中有些函数如gzopen, gzclose之类的在我用gcc编译的时候出现了rn/tmp/ccGlj7Is.o(.text+0x4d6): In function 'main':rn:undefined reference to 'gzopen'的错误, 感觉应该还是函数库的事,我已经安装了zlib,查了很多网页都无法解决,但是似乎编译没有问题,是链接的问题,但是我不知道怎么处理,请各位帮忙。rnrn2.顺便问下,我还有一个parser.c的文件中用到了一个parser.h的头文件,我必须把parser.h放在与parser.c的同一目录下吗?还需要什么其他的操作让parser.c可以用到parser.h吗?rnrn由于小弟是linux新手,所以请各位尽量详细一点的说下步骤。rnrn以下是小弟的相关代码rn#include rn#include rn#include rn#include rnrnrnint main(int argc, char* argv[])rnrnrnFILE *fPosting, *fDocId, *indexFileList, *statistic;rngzFile *fData, *fIndex;rnchar *indexBuf;rnchar *dataFileNamePool,*indexPool;rnint len;rnchar * pch, *pch1;rnchar *url;rnuLong begin, length;rnint i=0;rnint docId=0,ret=0;rnchar *pool = (char*)malloc(100000000);rnchar *buf=(char*)malloc(10000000);rnchar* docIdBuf; rnchar *docIdBufPtr;rnchar *dataPtr;rnchar *data = (char*)malloc(100000000);rnchar *index = (char*)malloc(10000000);rnchar *dataFileName = (char*)malloc(32*sizeof(char));rnchar *indexFileName = (char*)malloc(32*sizeof(char));rnlong long totalLength = 0;rnint totalDoc=0;rnint averageLength=0;rnchar * tmp=(char *)malloc(128);rnrn//open and read the indexFileList.txt rnrnindexFileList=fopen(argv[1],"r");rnif (indexFileList == NULL)rnrnprintf("%s can not be opened!\n", argv[1]);rnexit(1);rnrnrnfseek(indexFileList,0,SEEK_END);rnlen=ftell(indexFileList);rnfseek(indexFileList,0,SEEK_SET);rndataFileNamePool = (char*)malloc(len+1);rnfread(dataFileNamePool,len,1,indexFileList);rnfclose(indexFileList);rnrn//for each data file and its correspending index file,rn//first parer data page by page with the informtaion in index filern//then create postions and store in postionPoolrnrn//printf("%s",dataFileNamePool);rnpch = strtok_r (dataFileNamePool,"\n",&dataFileNamePool);rndocIdBuf =(char*)malloc(100000000);rnmemset(docIdBuf,'\0',sizeof(docIdBuf));rndocIdBufPtr = docIdBuf;rnwhile (pch != NULL)rn rn//memory allocaternmemset(data,'\0',sizeof(data));rnmemset(index,'\0',sizeof(index));rnmemset(dataFileName,'\0',sizeof(dataFileName));rnmemset(indexFileName,'\0',sizeof(indexFileName));rnrnrn//concate to get file namernstrcpy(dataFileName,"./");rnstrcat(dataFileName,pch);rnstrcat(dataFileName,"_data");rnstrcpy(indexFileName,"./");rnstrcat(indexFileName,pch);rnstrcat(indexFileName,"_index");rnprintf("reading data from file %s\n",dataFileName);rnprintf("reading index from file %s\n",indexFileName);rnrnrn//open dataFilernrnrnfData=gzopen(dataFileName,"r");rnif (fData == NULL)rnrnprintf("%s can not be opened!\n", dataFileName);rnexit(1);rnrngzread(fData,data,100000000);rngzclose(fData);rnrnrnrn//open index filernrnfIndex=gzopen(indexFileName,"r");rnif (fIndex == NULL)rnrnprintf("%s can not be opened!\n", index);rnexit(1);rnrngzread(fIndex,index,10000000);rngzclose(fIndex);rn 论坛

LINUX编译c程序的问题

07-17

各位大侠,我最近在编写一个接口程序。在windows下编译没什么问题,但是在linux下却不行,老是出现头文件的问题。具体问题如下,请各位帮忙处处主意——rn #include "string.h"rn#include "math.h"rn#include rn#include "stack-c.h"rnrnrnint intmax(char *fname)rnrnrnrn static int l1,m1=1, n1=1, m2=1, n2=1, l2, m3=1, n3=1, l3;rnrn static int minlhs=1, minrhs=2, maxlhs=1, maxrhs=2;rnrn rnrn CheckRhs(minrhs,maxrhs) ;rnrn CheckLhs(minlhs,maxlhs) ;rnrn rnrn GetRhsVar(1, "c", &m1, &n1, &l1); rnrnrn GetRhsVar(2, "c", &m2, &n2, &l2);rnrnrnrn CreateVar(3, "c", &m3, &n3, &l3);rnrnrnrnmain(cstk(l1),m1,n1,cstk(l2),m2,n2,cstk(l3));rnrnrnrn LhsVar(1)= 3;rnrn return 0;rnrnrnrnrnint main(int argc,char *argv[])rn rn int i = 0;rn while(--argc>0)rn printf("%s\n",argv[++i]);rn return 0;rn rn rn编译之后出现——rn 在包含自 5.c:4 的文件中:rnstack-c.h:8:27: 错误: graphics/Math.h:No such file or directoryrnIn file included from stack-c.h:15,rn from 5.c:4:rnstack-def.h:27: 警告: 函数声明中出现形参名却未指定类型rnstack-def.h:33: 警告: 函数声明中出现形参名却未指定类型rnstack-def.h:33: 错误: 与 ‘C2F’ 类型冲突rnstack-def.h:27: 错误: ‘C2F’ 的上一个声明在此rnstack-def.h:39: 警告: 函数声明中出现形参名却未指定类型rnstack-def.h:39: 错误: 与 ‘C2F’ 类型冲突rnstack-def.h:27: 错误: ‘C2F’ 的上一个声明在此rnstack-def.h:43: 警告: 函数声明中出现形参名却未指定类型rnstack-def.h:43: 错误: 与 ‘C2F’ 类型冲突rnstack-def.h:27: 错误: ‘C2F’ 的上一个声明在此rnstack-def.h:47: 警告: 函数声明中出现形参名却未指定类型rnstack-def.h:47: 错误: 与 ‘C2F’ 类型冲突rnstack-def.h:27: 错误: ‘C2F’ 的上一个声明在此rnstack-def.h:51: 警告: 函数声明中出现形参名却未指定类型rnstack-def.h:51: 错误: 与 ‘C2F’ 类型冲突rnstack-def.h:27: 错误: ‘C2F’ 的上一个声明在此rnstack-def.h:56: 警告: 函数声明中出现形参名却未指定类型rnstack-def.h:56: 错误: 与 ‘C2F’ 类型冲突rnstack-def.h:27: 错误: ‘C2F’ 的上一个声明在此rnstack-def.h:61: 警告: 函数声明中出现形参名却未指定类型rnstack-def.h:61: 错误: 与 ‘C2F’ 类型冲突rnstack-def.h:27: 错误: ‘C2F’ 的上一个声明在此rnstack-def.h:66: 警告: 函数声明中出现形参名却未指定类型rnstack-def.h:66: 错误: 与 ‘C2F’ 类型冲突rnstack-def.h:27: 错误: ‘C2F’ 的上一个声明在此rnIn file included from 5.c:4:rnstack-c.h:23: 错误: expected specifier-qualifier-list before ‘integer’rnstack-c.h:32: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:33: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:58: 错误: expected specifier-qualifier-list before ‘integer’rnstack-c.h:64:28: 错误: interf/stack1.h:No such file or directoryrnstack-c.h:65:28: 错误: interf/stack2.h:No such file or directoryrnstack-c.h:66:28: 错误: interf/stack3.h:No such file or directoryrnstack-c.h:105: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:232: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:235: 错误: expected specifier-qualifier-list before ‘interfun’rnstack-c.h:274: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:280: 错误: 与 ‘C2F’ 类型冲突rnstack-def.h:27: 错误: ‘C2F’ 的上一个声明在此rnstack-c.h: 在函数 ‘C2F’ 中:rnstack-c.h:280: 错误: expected declaration specifiers before ‘__PARAMS’rnstack-c.h:281: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:282: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:283: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:285: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:286: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:288: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:289: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:290: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:292: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:294: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:295: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:297: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:298: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:299: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:301: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:302: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:304: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rnstack-c.h:306: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__PARAMS’rn5.c:8: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘’ tokenrn5.c:28: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘’ tokenrn5.c:33: 错误: expected ‘’ at end of inputrnrnrnrn 大家帮帮忙吧,都会被这个问题折磨疯了。拜托了!rn 论坛

没有更多推荐了,返回首页