linux c语言编程环境搭建

inux c语言编程环境搭建

需要如下软件和开发包:
terminal 终端
gcc //编译器
cpp 
libgcc
libc6 //库 标准库 数学函数  在libc.so.6目录下
binutils //连接工具
    /usr/bin/size
    /usr/bin/ar
    /usr/bin/objdump
    /usr/bin/strings
    /usr/bin/as
    /usr/bin/ld
locals //提供本地支持
libc6-dev //c共享库 头文件
glibc-doc //文档
glibc-doc-reference //参考手册
manpages-dev //man 函数用法
make //维护源代码
make-doc
gdb //调试程序
vim //编辑器
indent //格式化源代码


简单的hello world代码编写执行过程演示:
用vi编辑代码
root@xuanfei-desktop:~/cpropram# vi hello.c
用indent格式化代码
root@xuanfei-desktop:~/cpropram# indent -kr hello.c
查看代码内容
root@xuanfei-desktop:~/cpropram# cat hello.c

#include <stdio.h>
int main(int argc, char **agv)
{
    printf("hello world\n");
    return 0;
}

查看原目录下有多少文件
root@xuanfei-desktop:~/cpropram# ls

hello.c  txt

编译hello.c文件
root@xuanfei-desktop:~/cpropram# gcc -Wall hello.c

再次查看
root@xuanfei-desktop:~/cpropram# ls

hello.c hello.c~ a.out txt
运行编译后生成的二进制可执行文件
root@xuanfei-desktop:~/cpropram# ./a.out

结果
hello world

int main(int argc,char *argv[]) 是lnux下c编程的标准写法,argc 是外部命令参数的个数,argv[] 存放各参数的内容,下面我们看下argc argv的用法
实例演示一、
查看代码:
root@xuanfei-desktop:~/cpropram# cat 1.c
#include <stdio.h>
int main(int argc, char **argv)
{
    printf("hello\n");
    if (argc < 2)
        return -1;
    else {
        printf("argc=%d argv[0]=%s argv[1]=%s\n", argv, argv[0], argv[1]);
    }
    return 0;
}

运行:
root@xuanfei-desktop:~/cpropram/1# ./a.out xuan fei

结果:
hello
argc=3 argv[0]=./a.out argv[1]=xuan

argc的值是 3,代表参数的总数,分别为:“./a.out” “xuan” “fei”
argv[0]是"./a.out"
argv[1]是"xuan"


实例演示二、
查看代码
root@xuanfei-desktop:~/cpropram# cat argc.c
#include <stdio.h>
int main(int argc, char **argv)
{
    int i;
    for (i = 0; i < argc; i++)
    printf("argv [%d] is %s\n", i, argv[i]);
    return 0;
}


运行:
root@xuanfei-desktop:~/cpropram# ./a.out my name is xuanfei
结果:
argv [0] is ./a.out
argv [1] is my
argv [2] is name
argv [3] is is
argv [4] is xuanfei

这样就可以清楚得看到argv 的内容:)

以上内容根据周立发linux视频教程所做的笔录,为了方便大家理解,建议大家可以到下面的连接下载观看。
周立发 linux 视频教程下载(不定期持续更新
转自:http://blog.chinaunix.net/u/29321/showart_344262.html

猜你喜欢

转载自yiranwuqing.iteye.com/blog/779575