文件包含
# 文件包含
C 语言的编译过程
预处理就是宏的事情,常见的就是以小博大的事情,最常见的就是文件包含:
include
,这些头文件非常的多。
#include <stdio.h>
int main() {
puts("hello world");
return 0;
}
1
2
3
4
5
6
2
3
4
5
6
一个简单的例子,puts
函数就在stdio
头文件中进行了声明和函数原型。我们可以替换为:
//
// Created by virus on 2022/5/28.
//
int __cdecl puts(char const*);
int main() {
puts("hello world");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
所以这里,我们引用头文件只是为了方便,就是为了引用我们需要的一些函数原型,导入一个头文件,最后编译会展开对应的内容都引入进来来代替头文件部分,最后展开的内容还是根据自己的编译器有关。
# 自定义头文件
这里会有一个注意点:
使用双引号导入头文件的特点:
#include "xxx.h"
- 首先查找当前源文件所在路径
- 查找工程的头文件搜索路径
使用<>
会直接查找工程的头文件搜索路径,如果此时在CMakeLists.txt
文件中设置
cmake_minimum_required(VERSION 3.19)
get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(REPLACE " " "_" ProjectId ${ProjectId})
project(${ProjectId} C)
set(CMAKE_C_STANDARD 11)
# 这一行
include_directories("include")
file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
foreach(file ${files})
get_filename_component(name ${file} NAME)
add_executable(${name} ${file} src/factorial.c)
endforeach()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
即可使用<>
来进行引用
自定义两个目录:include
和src
,include
放头文件,src
放源文件
新建阶乘的头文件:
//
// Created by virus on 2022/5/29.
//
#ifndef HELLOWORLDC_INCLUDE_FACTORIAL_H_
#define HELLOWORLDC_INCLUDE_FACTORIAL_H_
// 在中间部分写对应的实现方法的原型
unsigned int Factorial(unsigned int n);
unsigned int FactorialByIteration(unsigned int n);
#endif //HELLOWORLDC_INCLUDE_FACTORIAL_H_
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
新建阶乘的源文件
//
// Created by virus on 2022/5/29.
//
// 引入路径必须使用双引号,尖括号不行
#include "../include/factorial.h"
unsigned int Factorial(unsigned int n) {
if (n == 0) {
return 1;
} else {
return n * Factorial(n - 1);
}
}
unsigned int FactorialByIteration(unsigned int n) {
unsigned int result = 1;
unsigned int i = n;
for (; i > 0; i--) {
result *= i;
}
return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
最后是使用的示例
//
// Created by virus on 2022/5/29.
//
#include "stdio.h"
#include "include/factorial.h"
int main(void) {
printf("3!=%d\n", Factorial(3));
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
如果这里设置了CMakeLists.txt
的include_directories("include")
,这里引入的头文件可以使用尖括号来代替:#include <factorial.h>
注意点
所以说,双引号是兼容的,但是呢,使用双引号,编译的时候会首先在本地查一遍,会有一个资源的开销,也正因为如此,如果你知道这个头文件在搜索路径下,就使用<>
进行引入。
编辑 (opens new window)
上次更新: 2022/05/29, 23:22:49