当前位置:网站首页>Server body 21: pre compilation processing by different compilers (a brief introduction to MSVC and GCC)
Server body 21: pre compilation processing by different compilers (a brief introduction to MSVC and GCC)
2022-07-28 19:10:00 【Xie Baiyu】
List of articles
One 、 background
- Most compilers support precompiling
In most of c/c++ Compilers support precompiled headers , for example :gcc,clang,msvc etc. - Precompile function
Used to optimize c++ Speed of code compilation , After all c++ If the header file contains the template definition , Compilation speed is very slow - Precompiled approach
Most general header files can be placed in one header.h in , Compile other source code before compiling it , Later code can reuse this part of the precompiled header , It can greatly reduce the frequent redundant compilation of header files .
Two 、msvc Precompiled header processing for
Precompiled header in msvc Is very common in projects , I often see something like stdafx.cpp, stdafx.h The file of , For this purpose , and msvc The compiler compiles stdafx.cpp To generate precompiled header files stdafx.pch Of .
1) summary
Generally, under the same project stdafx.cpp Is to create a precompiled header (/Yc), other .cpp Files are using precompiled headers (/Yu), all .h The file doesn't care about this
2) Specific use
Overall process Preview
1) Create precompiled header command
2) Other files use this stdafx.pch Methods
3) link obj Attention
4) Differences with other compilersCreate precompiled header command
$ cl.exe -c -Yc -Fpstdafx.pch -Fostdafx.obj stdafx.cpp
1)-Yc Is to create a precompiled header stdafx.pch
2)-Fp To specify the *.pch Output file path of
3)-Fo Specify compile stdafx.cpp Generate object file
- How to use this for other files stdafx.pch?
By way of stdafx.h Pass in -Yu To tell the compiler , Compile the current code , Ignore #include “stdafx.h”, Directly use the compiled stdafx.pch file .
cl.exe -c -Yustdafx.h -Fpstdafx.pch -Fotest.obj test.cpp
- At the end of the link
Need to put :stdafx.obj, test.obj All connected , This is also related to gcc, clang The compiler is different .
link.exe -out:test test.obj stdafx.obj
- Other considerations
1) notes : Be sure stdafx.obj Also on the link , although stdafx.cpp Only for generating stdafx.pch, But object files are also required .
2) There's another one with gcc, clang The difference is ,msvc Of -Yu Appoint stdafx.h Must be #include "stdafx.h" Header file name in , Not a file path .
3、 ... and 、gcc Precompiled header file processing for
1)gcc Search for stdafx.pch Rules for file paths
1) from stdafx.h In the directory , lookup stdafx.h.pch Does the file exist
2) from -I The header file search path of find find find stdafx.h.pch
2) compile
- Overall process
1) Compile header file generation pch file :
2) Using precompiled header files :
- Compile header file generation pch file :
gcc -c -o stdafx.pch stdafx.h
- Using precompiled header files :
gcc -c -include stdafx.h -o test.o test.cpp
Four 、 remarks (C and C++ about *.h The difference between compilation )
1)gcc、clang about *.h Header file compilation , The default is as c Used by precompiled headers , This one c++ Of pch It's different .
2)*.h The header file compilation of cannot be given c++ The code uses , If you want to generate c++ Usable pch file , You have to tell the compiler , How to compile stdafx.h
- Method
This can be done through -x c+±header Parameters to solve :
gcc -c -x c++-header -o stdafx.pch stdafx.h
- Of course, it can also be solved by modifying the suffix :
gcc -c -o stdafx.pch stdafx.hpp
边栏推荐
- JVM tuning
- 2、 Uni app login function page Jump
- BM14 链表的奇偶重排
- 2022年牛客多校第2场 J . Link with Arithmetic Progression (三分+枚举)
- 11 年膨胀 575 倍,微信为何从“小而美”变成了“大而肥”?
- Example of observer mode of C -- ordering milk
- Swiftui component how to implement textfield of hidden part of phone number mask (tutorial includes source code)
- 服务器正文21:不同编译器对预编译的处理(简单介绍msvc和gcc)
- How long does software testing take?
- How to choose between software testing and software development?
猜你喜欢

How to obtain data on mobile phones and web pages after the SCM data is uploaded to Alibaba cloud Internet of things platform?

The open source of "avoiding disease and avoiding medicine" will not go far
![[machine learning] support vector machine classification](/img/6d/e4fb9b5bf82362edcebbf366f9b73e.png)
[machine learning] support vector machine classification

The ever-changing pointer ----- C language

JVM four reference types

What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?

The switching language of unity causes an error: system FormatException:String was not recognized as a valid DateTime.

BM16 删除有序链表中重复的元素-II

Structure and working principle of thyristor

Example of observer mode of C -- ordering milk
随机推荐
直播平台软件开发,js实现按照首字母排序
What if svchost.exe of win11 system has been downloading?
Kotlin:sealed Class detailed explanation of sealed class
QT user defined control user guide (flying Qingyun)
“讳疾忌医”的开源走不远
Is the software testing training institution reliable?
Why app uses JSON protocol to interact with server: serialization related knowledge
Overview and working principle of single chip microcomputer crystal oscillator
[R language - basic drawing]
pytest 自定义HOOK函数
uwb模块实现人员精确定位,超宽带脉冲技术方案,实时厘米级定位应用
QT & OpenGL lighting
ECS 5 workflow
EasyCVR接入设备后播放视频出现卡顿现象的原因分析及解决
Redis advantages and data structure related knowledge
优麒麟系统安装BeyondComare
[machine learning] support vector machine classification
Efficiency comparison of JS array splicing push() concat() methods
视频融合云服务EasyCVR平台白名单功能如何使用?
N32 replaces STM32. Don't ignore these details!