C项目构建入门

多文件编译
max.c

int max(int a, int b)
{
    if(a>b) {
        return a;
    }
    return b;
}

hello.c

#include<stdio.h>

int main()
{
    int a1 = 33;
    int a2 = 21;
    int maxnum = max(a1,a2);
    printf("the max value is %d", maxnum);
    return 0;
}

执行:

# gcc max.c hello.c -o main.out   // 多文件编译

也可以使用 在hello.c文件中 添加#include “max.c"文件编译,这个是标准形式。

如果项目很大,文件需要分开编译

# gcc -c max.c -o max.o   // 单独编译文件

hello.c 文件中就不能包含 #include “max.c”。

# gcc max.o hello.c   // 链接编译的文件

以上代码文件我们可以直接观察源代码, 如果是类似于系统库文件(别人编写的),并且已经被编译了,hello.c就不知道max()这个函数名。 所以引入了声明 ( .h ) 文件,说明函数定义,方便团队开发交流。 编译时候仍然需要带上 max.o 文件。

max.h

int max(int a, int b);

hello.c

#include <stdio.h>
#include “max.h”

int main()
{
    int a1 = 33;
    int a2 = 21;
    int maxnum = max(a1,a2);
    printf("the max value is %d", maxnum);
    return 0;
}
# gcc max.o hello.c    // 编译仍然需要 max.o 文件

makeFile文件编写

如果项目有几千个函数文件,执行编译 gcc max.c min.c middle.c hello.c -o run.o 是很不合理的。

make工具
约定 Makefile格式》》》目标文件: 依赖文件,【换行tab】 对应的命令。

Makefile

#this is  Makefile

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

当前目录文件内容如下:

max.h

int max(int a, int b);

min.h

int min(int a, int b);

max.c

int max(int a, int b)
{
    if(a>b) {
        return a;
    }
    return b;
}

min.c

int min(int a, int b)
{
    if(a<b) {
        return a;
    }
    return b;
}

hello.c

#include<stdio.h>
#include "max.h"
#include "min.h"

int main()
{
    int a1 = 33;
    int a2 = 21;
    int maxnum = max(a1,a2);
    int minnum = min(a1,a2);
    printf("the max value is %d , \n the min value is %d .", maxnum, minnum);
    return 0;
}
# ls                                                                                                                                    
hello.c  Makefile  max.c  max.h  min.c  min.h

# make   // 执行make                                                                                                                                    
gcc -c max.c
gcc -c min.c
gcc max.o min.o hello.c

# ls                                                                                                                                    
a.out  hello.c  Makefile  max.c  max.h  max.o  min.c  min.h  min.o

生成了 o.out 文件,在Makefile中编写的时候,gcc命令没有指定文件名:

hello.out:max.o min.o hello.c
    gcc max.o min.o hello.c

修改为:

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

再次编译一次,此次将只会编译我们修改的部分。

# make                                                                                                                                   
gcc max.o min.o hello.c -o hello.out

# ls                                                                                                                                    
a.out  hello.c  hello.out  Makefile  max.c  max.h  max.o  min.c  min.h  min.o

C语言main函数的完整形式:

// main参数可以省略
int main(int argv, char* argc[])
{
    printf(“Hello world”);
    return 0;
}

遍历参数:

#include <stdio.h>

int main(int argv, char* argc[])
{

    printf("argv is %d \n", argv);
    int i;
    for (i; i < argv; i++) {
        printf("argc[%d] is %s \n", i, argc[i]);
    }

    return 0;
}

编译执行

# gcc main.c -o main.out
# ./main.out 12 34 abc d                                                                                                
argv is 5
argc[0] is ./main.out
argc[1] is 12
argc[2] is 34
argc[3] is abc
argc[4] is d

Linux 启动一个程序的时候,会给我们创建 stdin,stdout,stderr 三个流文件。
标注输入设备默认是 键盘,stdout 默认是显示器,可以需改为 打印机,声卡等。

fprintf(); 文件操作

printf() 函数是对 sprintf() 的封装:

printf(“please input the value a: \n”);
相当于:
fprinrf(stdout, “please input the value a: \n");

scanf(“%d”, &a);
相当于:
fscanf(stdin, “%d”, &a);

错误流
fscanf(stderr, “%d”, &a);

标注输入输出流,标准错误流 重定向 ““<"”,“>” , “>>”, “2>” , “2>&1” // linux系统支持的功能,配合 C 语言 stdin,stdout, stderr, 和程序的返回值(非0为错误)配合使用,使得程序正常运行。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值