当前位置:网站首页>服务器正文21:不同编译器对预编译的处理(简单介绍msvc和gcc)
服务器正文21:不同编译器对预编译的处理(简单介绍msvc和gcc)
2022-07-28 17:13:00 【谢白羽】
一、背景
- 大部分编译器都支持预编译
在的大部分c/c++编译器都是支持预编译头的,例如:gcc,clang,msvc等 - 预编译作用
用于优化c++代码的编译速度,毕竟c++的头文件如果包含了模板定义的话,编译速度是很慢的 - 预编译的做法
能够吧大部分通用的头文件放置在一个header.h中,在其他源码编译之前预先对其进行编译,之后的代码都能重用这部分预编译头,就可以极大程度上减少频繁的头文件冗余编译。
二、msvc的预编译头处理
预编译头在msvc的项目中很常见,经常会看到类似stdafx.cpp, stdafx.h的文件,就是用于此目的,而msvc编译器是通过编译stdafx.cpp来生成预编译头文件stdafx.pch的。
1)总结
一般同一项目下的stdafx.cpp是创建预编译头(/Yc),其他.cpp文件是使用预编译头(/Yu),所有.h文件不关心这个
2)具体使用
总体流程预览
1)创建预编译头命令
2)其他文件使用这个stdafx.pch的方法
3)链接obj的注意
4)与其他编译器的区别创建预编译头命令
$ cl.exe -c -Yc -Fpstdafx.pch -Fostdafx.obj stdafx.cpp
1)-Yc就是创建预编译头stdafx.pch
2)-Fp来指定*.pch的输出文件路径
3)-Fo指定编译stdafx.cpp生成对象文件
- 其他文件如何使用这个stdafx.pch?
通过将stdafx.h传入-Yu来告诉编译器,编译当前代码,忽略#include “stdafx.h”,直接使用已经编译好的stdafx.pch文件。
cl.exe -c -Yustdafx.h -Fpstdafx.pch -Fotest.obj test.cpp
- 最后链接的时候
需要把:stdafx.obj, test.obj都连接上才行,这个也是和gcc, clang编译器不同的地方。
link.exe -out:test test.obj stdafx.obj
- 其他注意点
1)注:一定要吧stdafx.obj也链接上哦,虽然stdafx.cpp仅用于生成stdafx.pch,但是对象文件也是需要。
2)还有个跟gcc, clang有区别的地方是,msvc的-Yu指定stdafx.h必须是#include "stdafx.h"中的头文件名字,不是文件路径哦。
三、gcc的预编译头文件处理
1)gcc搜索stdafx.pch文件路径的规则
1)从stdafx.h所在目录中,查找stdafx.h.pch文件是否存在
2)从-I的头文件搜索路径找查找stdafx.h.pch
2)编译
- 总体流程
1)编译头文件生成pch文件:
2)使用预编译头文件:
- 编译头文件生成pch文件:
gcc -c -o stdafx.pch stdafx.h
- 使用预编译头文件:
gcc -c -include stdafx.h -o test.o test.cpp
四、备注(C和C++对于*.h编译的区别)
1)gcc、clang对于*.h的头文件编译,默认是作为c预编译头来使用的,这跟c++的pch是不一样的。
2)*.h的头文件编译无法给c++的代码使用,如果要生成c++可用的pch文件,必须要告诉编译器,如何去编译stdafx.h
- 方法
这个可以通过-x c+±header参数来解决:
gcc -c -x c++-header -o stdafx.pch stdafx.h
- 当然也可以通过修改后缀名来解决:
gcc -c -o stdafx.pch stdafx.hpp
边栏推荐
- uwb模块实现人员精确定位,超宽带脉冲技术方案,实时厘米级定位应用
- 【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码
- My creation anniversary -- July 25th, 2022
- Can I get employed after two months of software testing training?
- 【滤波跟踪】基于EKF、时差和频差定位实现目标跟踪附matlab代码
- jvm四种引用类型
- @The difference between Autowired and @resource
- QT - CPP database operation
- Easynlp Chinese text and image generation model takes you to become an artist in seconds
- Four years later, Debian finally recaptured the "debian.community" domain name!
猜你喜欢

Win11系统svchost.exe一直在下载怎么办?

Introduction and advanced MySQL (III)

CTR click through rate prediction practice project of advertising recommendation!

Special Lecture 6 tree DP learning experience (long-term update)

【图像隐藏】基于DCT、DWT、LHA、LSB的数字图像信息隐藏系统含各类攻击和性能参数附matlab代码

“讳疾忌医”的开源走不远

cv5200无线WiFi通信模块,视频图像传输无线化,实时无线通信技术

APP为什么用JSON协议与服务端交互:序列化相关知识

Win11怎么调亮度?Win11调屏幕亮度的四种方法

Easynlp Chinese text and image generation model takes you to become an artist in seconds
随机推荐
Introduction and advanced MySQL (7)
Is two months of software testing training reliable?
Overview and working principle of single chip microcomputer crystal oscillator
Redis advantages and data structure related knowledge
112. 使用自开发的代理服务器解决 SAP UI5 FileUploader 上传文件时遇到的跨域访问错误
视频融合云服务EasyCVR平台白名单功能如何使用?
Win11怎么调亮度?Win11调屏幕亮度的四种方法
LeetCode_ 63_ Different paths II
Swiftui component how to implement textfield of hidden part of phone number mask (tutorial includes source code)
Special Lecture 6 tree DP learning experience (long-term update)
Implementation of grayscale publishing with haproxy
QT running image
LeetCode_ 96_ Different binary search trees
Xiaobai must see the development route of software testing
My creation anniversary -- July 25th, 2022
Introduction and advanced MySQL (III)
Software testing dry goods
ECS 5 workflow
全新升级!《云原生架构白皮书 2022 版》重磅发布
Chinese enterprise service industry market in 2022