Linux系统下编译连接C源代码

 

gcc test.c -o test        一步到位的编译指令  得到 test 文件
gcc test.c          得到 test.out 文件

 

gcc -g -c test.c -o test    只生成目标文件(.obj文件),没有生成可执行文件(也就是说test是.obj文件)
gcc -g test.c -o test        生成可执行文件(可以通过./test运行程序)

 

-g:生成调试信息。GNU 调试器可利用该信息。
-c:只编译并生成目标文件。(没有中间文件生成和可执行文件,如列表文件、可执行文件)
-o:设置生成的可执行程序的名字为test
上面的命令会产生可执行程序:test
在Terminal中输入:./test 就可以运行该程序了。

 

gcc main.c
gcc main.c -o main.out
gcc -c main.c -o main.o
gcc max.o min.o hello.c 
gcc max.o min.o hello.c -o hello.out


gcc main.c -o main.out && ./main.out    只有当第一个命令执行成功的情况下,下一个命令才会执行
echo $?          查看返回结果

 main函数中的参数:

int main(int argv, char **argc[])
{
    printf("argv is %d\n",argv);
    int i;
    for(i=0;i<argv;i++){
         printf("argc[%d] is %s\n", i, argc[i]);
    }
    return 0;
}


int main(int argc, char **argv[])
{
    int port = 0;
    printf("program begin\n");
    if (argc != 3) {
        printf
            ("程序参数个数不对!正确输入方法如下:\n%s ip-address port\n",
             argv[0]);
        exit(0);
    }
    if (atoi(argv[2]) < 0 || atoi(argv[2]) > 65535) {
        printf
            ("端口输入不对!应该是0-65535的值。程序取默认值3456\n");
        port = 3456;
    } else
        port = atoi(argv[2]);
    printf("命令如下:%s %s %d\n", argv[0], argv[1], port);
    return 0;
}    

 

posted @ 2015-07-23 18:47  坠落鱼  阅读(612)  评论(0编辑  收藏  举报