C++ - 头文件基础(常用标准库头文件、自定义头文件、头文件引入方式、防止头文件重复包含机制)

一、头文件

  1. 在 C++ 中,头文件(.h)用于函数声明、类定义、宏定义等等

  2. 在 Visual Studio 中,头文件通常放在头文件目录中,头文件实现通常放在源文件目录中


二、常用标准库头文件

1、输入输出
  • <iostream> 标准输入输出流
cpp 复制代码
#include <iostream>

using namespace std;

int main() {

	cout << "Hello World" << endl;

	return 0;
}
2、容器
  1. <string> 字符串处理
cpp 复制代码
#include <string>

using namespace std;

int main() {

	string s = "Hello World";

	return 0;
}
  1. <vector> 动态数组
cpp 复制代码
#include <vector>

using namespace std;

int main() {

	vector<int> nums = { 1, 2, 3 };

	return 0;
}
3、多线程
  • <thread> 线程支持
cpp 复制代码
#include <iostream>
#include <thread>

using namespace std;

void threadFunction() {
	std::cout << "Hello Thread" << endl;
}

int main() {

	thread t(threadFunction);

	t.join();

	return 0;
}

三、自定义头文件

  1. 头文件 math_utils.h
cpp 复制代码
#pragma once

#include <iostream>

namespace math {

	int square(int x);

	void printSquare(int x);
}
  1. 头文件实现 math_utils.cpp
cpp 复制代码
#include "math_utils.h"

namespace math {

    int square(int x) {
        return x * x;
    }

    void printSquare(int x) {
        std::cout << "Square of " << x << " is " << square(x) << std::endl;
    }
}
  1. 测试代码 math_utils_test.cpp
cpp 复制代码
#include "math_utils.h"
#include <iostream>

int main() {
	
	std::cout << math::square(5) << std::endl;
	math::printSquare(4);

	return 0;
}

四、头文件引入方式

1、使用双引号
cpp 复制代码
#include "math_utils.h"
  1. 首先,编译器在当前源文件所在目录搜索

  2. 然后,编译器在指定的包含路径中搜索(例如,-I 选项指定的路径)

  3. 最后,编译器在标准系统包含路径中搜索

  4. 这种方式通常用于包含用户自定义的头文件

2、使用尖括号
cpp 复制代码
#include <iostream>
  1. 编译器不会在当前目录搜索

  2. 编译器直接在标准系统包含路径中搜索

  3. 这种方式通常用于包含标准库头文件


五、防止头文件重复包含机制

1、基本介绍
  1. #pragma once 是 C++ 中用于防止头文件重复包含的编译器指令

  2. #include "math_utils.h" 这行代码来举例,重复包含就是写了多次这行代码

  3. 头文件使用 #pragma once 后,当编译器首次包含头文件时,会记录该头文件的唯一标识(完整路径)

  4. 后续再遇到相同的包含头文件时,编译器会直接跳过其内容

  5. #pragma once 是传统头文件保护宏(#ifndef / #define /#endif)的现代替代方案

2、演示
(1)未防止重复包含
  1. 头文件 math_utils.h,未添加 #pragma once
cpp 复制代码
int globalVar = 10;
  1. 测试代码 my_header_test.cpp,重复包含了头文件
cpp 复制代码
#include "my_header.h"
#include "my_header.h"

#include <iostream>

using namespace std;

int main() {

	cout << globalVar << endl;

	return 0;
}
复制代码
# 输出结果

C2374	"globalVar": 重定义;多次初始化	
(2)防止重复包含
  1. 头文件 math_utils.h,添加了 #pragma once
cpp 复制代码
int globalVar = 10;
  1. 测试代码 my_header_test.cpp,重复包含了头文件
cpp 复制代码
#include "my_header.h"
#include "my_header.h"

#include <iostream>

using namespace std;

int main() {

	cout << globalVar << endl;

	return 0;
}
复制代码
# 输出结果

10
相关推荐
quantdash_cc4 分钟前
历史数据断层与REST请求慢到崩溃?QuantDash高性能量化数据API终极解决方案
开发语言·python·缓存·php·量化·quantdash
breeze jiang4 分钟前
Next.js 16 App Router 实战:从 About、Blog 动态路由到全局 404
开发语言·javascript·ecmascript
Loongproxy4 分钟前
2026年动态IP代理真实可用率怎么横评?爬虫选代理的关键是什么
服务器·网络·网络协议·tcp/ip
熊野君8 分钟前
Prompt / RAG / 规则 / Skills —— 定位、协作与实现路线
开发语言·python
小蒜学长9 分钟前
vue旅游攻略网站(代码+数据库+LW)
java·数据库·vue.js·spring boot·后端·旅游
mengge.cloud11 分钟前
LVS 小白完整教程(Linux虚拟服务器负载均衡)
linux·运维·服务器·负载均衡·lvs
秦渝兴14 分钟前
LVS-DR 高可用集群实战:NFS + Web + DNS + Keepalived 全套架构
linux·服务器·网络·lvs
凤山老林17 分钟前
轻量级规则引擎落地:Spring Boot 集成 LiteFlow 实现动态业务编排与热更新
java·spring boot·后端·规则引擎·liteflow
skr爱码士20 分钟前
05_Qt 核心模块概览——Qt Core、Gui、Widgets、Quick 的职责划分
c++·qt·系统架构·客户端
IT_陈寒23 分钟前
Python装饰器把我坑惨了,原来这样用才不掉链子
前端·人工智能·后端