当前位置:网站首页>C language: 7. How to use C language multi source files
C language: 7. How to use C language multi source files
2022-07-27 19:33:00 【Brother rabbit head】
c Language :7、 Custom header file
1、 Example 1: Multiple source file examples
A larger project should not write all the code in the same file , It is usually divided into several documents . Therefore, you need to compile multiple files .
Example 1 Introduced the use of gcc Command to compile multiple files
1、 Define function source file /hello.c
#include <stdio.h>
int main() {
int maxNum = max(33,20);
printf("the max value is %d", maxNum);
return 0;
}
2、 Define function source file /max.c
int max(int n1, int n2)
{
if(n1 > n2){
return n1;
} else{
return n2;
}
}
3、 compiling
linux Environmental use gcc Command compilation main.out Executable file , And implement gcc max.c hello.c -o main.out
The results are as follows 
2、 Compile infrequently changed files in advance
Because if the whole project is compiled, it will be very slow , Therefore, it is generally possible to compile infrequently changed files directly into .o file ;
As an example 1 Documents in hello.c、max.c You can use the following method , Improve compilation speed .
1、 Compile infrequently changed files
gcc -c max.c max.o
The compiled file is not executable 
2、 take main The entry and the compiled .o Compile files together
Compile the file gcc max.o hello.c
Execute the compiled executable 
2、
3、 Example of separation of header file and function definition
If multiple people cooperate in the development , If other colleagues want to use the code I wrote , Header files can be declared (.h) The return value of the function 、 The parameter type is written .
In this way, he only has .o In the case of documents , Also know how to use the function I wrote .
1、 Define header file /include/factorial.h
Declare the function in the header file
#ifndef INC_3_FACTORIAL_H
#define INC_3_FACTORIAL_H
unsigned int Factorial(unsigned int n);
unsigned int FactorialByIteration(unsigned int n);
#endif //INC_3_FACTORIAL_H
2、 Define source code file /src/factorial.c
Implement functions in the source file
#include "../include/factorial.h"
unsigned int Factorial(unsigned int n) {
if( n == 0){
return 1;
} else{
return n * Factorial( n - 1);
}
}
3、 stay /main.c Call in
#include <stdio.h>
#include "include/factorial.h"
int main() {
//puts("hello world");
printf("3 != %d\n", Factorial(2));
return 0;
}
4、cmake Add compiler in /CMakelists.txt
cmake_minimum_required(VERSION 3.20)
project(3 C)
set(CMAKE_C_STANDARD 99)
add_executable(3 main.c src/factorial.c)
5、 Execute the compiled file

边栏推荐
- Normal distribution, lognormal distribution, generation of normal random numbers
- Yanghui triangle
- C语言案例:密码设置及登录> 明解getchar与scanf
- kettle 分列、合并记录
- New system installation mysql+sqlyog
- sql 时间处理(SQL SERVER\ORACLE)
- The understanding of string in C.
- Word 2007+ tips
- golang设置国内镜像,vscode配置golang开发环境,vscode调试golang代码
- Power control
猜你喜欢

ES6学习笔记(1)——快速入门

golang设置国内镜像,vscode配置golang开发环境,vscode调试golang代码

Kinect for Unity3d----KinectManager

图的遍历的定义以及深度优先搜索和广度优先搜索(二)

Idea optimization strategy

成本高、落地难、见效慢,开源安全怎么办?

An article allows you to master threads and thread pools, and also solves thread safety problems. Are you sure you want to take a look?

c语言:clion调试方法

200行代码快速入门文档型数据库MonogoDB

Technology Summit 58 Liu Yuan in the same city was invited to attend qecon 2022 global software quality & effectiveness conference
随机推荐
[Luogu p3175] bitwise OR (min max inclusive) (high-dimensional prefix and / FWT)
Rename file with command line
ES6 new method
让你的聊天气泡丰富多彩
时间复杂度和空间复杂度
An article allows you to master threads and thread pools, and also solves thread safety problems. Are you sure you want to take a look?
Webmagic+selenium+chromedriver+jdbc grabs data vertically.
Unity shows Kinect captured shots
kettle EXCEL 累计输出数据
FZU1669 Right-angled Triangle【毕达哥拉斯三元组】
SSM project uses filter to realize login monitoring
SQL server top 关键字使用
Matrix of shell programming -- it's cute and cool
200行代码快速入门文档型数据库MonogoDB
带来高价值用户体验的低代码开发平台
kettle 分列、合并记录
C language case: password setting and login > clear solution getchar and scanf
c语言:12、gdb工具调试c程序
Role authorization --- complete the addition and deletion of secondary menus by adding and deleting primary menus
Using vscode to build u-boot development environment