标签: precompiled-headers

预编译 C++ 头文件是否会减慢链接时间?

我的 externalFiles 目录中有 100 个 .h 文件。我的源代码中大约有 10 个 .cpp 文件包含这些 .h 文件。

因此,我#include externalFiles/.*h从 .cpp 文件中删除了所有指令,并将它们写入通过 Cmake 包含的 pch.h 标头中target_precompile_headers(${PROJECT_NAME} PRIVATE pch.h)

通过 Cmake 使用预编译头检查了构建时间,而不是简单地将 pch.h 包含在我的 .cpp 文件中。我使用以下方法记录了构建时间: set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CMAKE_COMMAND} -E time")set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "${CMAKE_COMMAND} -E time")

使用预编译头构建时间

[1/2] 构建 CXX 对象 CMakeFiles/AlgoCoding.dir/main.cpp.obj

经过时间:11 秒。(时间),11.413 秒。(钟)

[2/2] 链接 CXX 可执行文件 AlgoCoding.exe

经过时间:54 秒。(时间),53.459 秒。(钟)

构建时间仅包括标头

[1/2] 构建 CXX 对象 CMakeFiles/AlgoCoding.dir/main.cpp.obj

经过时间:14秒。(时间),13.35 秒。(钟)

[2/2] 链接 CXX 可执行文件 …

c++ linker precompiled-headers build-time

8
推荐指数
1
解决办法
607
查看次数

预编译的头文件

预编译头文件究竟是什么?他们什么时候用的?

precompiled-headers

7
推荐指数
1
解决办法
623
查看次数

全局常量文件中的Objective-C typedef枚举

好的,这与问题"目标C中的常量"有关.

我创建了Constants.h及其相应的Constants.m文件:

// Constants.h
extern int const BOOKS; 
typedef enum SSDifficultyLevel {
    EASY = 0,
    MEDIUM = 1,
    HARD = 2
} SSDifficultyLevel;

// Constants.m
int const BOOKS = 66;
Run Code Online (Sandbox Code Playgroud)

我的问题:在Constants.h中enum可以做到这一点吗?代码正在编译好(到目前为止没有警告或错误),但我想知道这是否是正确的方法,因为相关问题中提供的解决方案涉及拆分常量的定义和声明.typedef

谢谢.

constants objective-c precompiled-headers

7
推荐指数
1
解决办法
5310
查看次数

如何在Qt项目中使用预编译头文件

我的IDE:Visual Studio 2010,我使用Qt加载项为VS,Qt ver.4.8.1

在我的Qt项目中尝试创建预编译头(pch)时,我遇到了问题.

我在非Qt项目中创建pch的方法是:
1.创建标题;
2.包含将在头文件中预编译的文件;
3.对于项目状态中的每个源文件,如果它将使用pch;
4.对于pch的项目状态创建中的一个源文件; 5.在所有源文件中包含pch.

由于这些操作对Qt项目失败,我决定将pch发生的事情应该包含在MOC生成的所有文件中.

我在QtAssistant中阅读了有关预编译头文章的文章并执行了以下操作:
1.创建头文件;
2.对于项目集选项中的所有.cpp文件,使用pch和一个创建
3.转换为qmake生成的项目
4.我运行了qmake -project
5.我修改了生成的.pro文件,这里是:

TEMPLATE = app
TARGET = 
DEPENDPATH += . GeneratedFiles
INCLUDEPATH += .
PRECOMPILED_HEADER = StdAfx.h
QT += network

# Input
HEADERS += server.h StdAfx.h
FORMS += server.ui
SOURCES += main.cpp server.cpp StdAfx.h.cpp
RESOURCES += server.qrc
Run Code Online (Sandbox Code Playgroud)
  1. 我跑了qmake
  2. 打开.pro文件并尝试构建它并得到错误:

    错误2错误C1083:无法打开包含文件:'StdAfx.h':没有这样的文件或目录

我做错了什么?

c++ qt visual-studio-2010 precompiled-headers

7
推荐指数
2
解决办法
1万
查看次数

收集共同包含在一个文件中 - 良好的做​​法?

我正在努力学习如何处理很多包含,并且仍然保持我的代码整洁.

我正在编写一个Qt应用程序,我把常用的文件(并没有改变)放在一个名为"Util.h"的文件中.

Util.h

#pragma once

#include <Core/IObserver.h>
#include <Core/Math.h>
#include <QAction>
#include <QDockWidget.h>
#include <QFileDialog>
#include <QGraphicsBlurEffect>
#include <QLabel.h>
#include <QMainWindow.h>
#include <QMenu.h>
#include <QMessageBox.h>
#include <QShortcut.h>
#include <QSignalMapper>
#include <QSound>
#include <QString>
#include <QTimer.h>
#include <QTreeView>
#include <QStandardItemModel>


// Path to icons
#define ICON_PATH                                               \
    "../../Assets/GUI/Icons/"
Run Code Online (Sandbox Code Playgroud)

然后我将它包含在几乎所有的头文件中

#include "Util.h"
#include "Manager_Docks.h"
#include "Manager_Tools.h"
#include "Manager_Console.h"
#include "ui_MainWindow.h"
...
Run Code Online (Sandbox Code Playgroud)
  • 有没有更好的方法呢?
  • 这会减慢编译时间吗?

我也想到只在每个.cpp中都包含Util.h,并且对于头文件有一个单独的Header_Util.h,看起来像这样

Header_Util.h

#pragma once

class IObserver;
class QAction;
class QDockWidget;
class QFileDialog; …
Run Code Online (Sandbox Code Playgroud)

c++ include header-files precompiled-headers forward-declaration

7
推荐指数
1
解决办法
2137
查看次数

C++我是否必须为每个源文件包含标准库?

我现在有点困惑,因为我计划在我的一个项目中首次包含多个源文件和头文件.
所以我想知道这是否是正确的方法?
我是否必须在每个直接使用它的源文件中包含字符串标头?
那么Visual C++要我包含的"stdafx.hpp"标题呢?

这会是要走的路吗?

main.cpp中

#include "stdafx.hpp"
#include <string> //?
#include <stringLib1.h>
#include <stringLib2.h>
using std::string;

//use a windows.h function here
//use a stringLib1 function here
//use a stringLib2 function here
Run Code Online (Sandbox Code Playgroud)

stringLib1.h

#include "stdafx.hpp"
#include <string>
using std::string;

class uselessClass1
{
public:
    string GetStringBack1(string myString);
};
Run Code Online (Sandbox Code Playgroud)

stringLib1.cpp

#include "stdafx.hpp"

string uselessClass1::GetStringBack1(string myString) {
    return myString;
}
Run Code Online (Sandbox Code Playgroud)

stringLib2.h

#include "stdafx.hpp"
#include <string>
using std::string;

class uselessClass2
{
public:
    string GetStringBack2(string myString);
};
Run Code Online (Sandbox Code Playgroud)

stringLib2.cpp

#include "stdafx.hpp"

string uselessClass2::GetStringBack2(string myString) { …
Run Code Online (Sandbox Code Playgroud)

c++ include standard-library header-files precompiled-headers

7
推荐指数
2
解决办法
1万
查看次数

是否有更简洁的方法来处理编译器错误C1076和C3859?

今天我一直在为我们的precomp.h文件添加一些库头文件.然后我尝试在调试中重新编译并得到这两个错误(从boost包括产生):

错误C3859:超出PCH的虚拟内存范围; 请使用'-Zm310'或更高版本的命令行选项重新编译

致命错误C1076:编译器限制:达到内部堆限制; 使用/ Zm指定更高的限制

所以我通过增加内存堆大小来修复它们.没问题.

我的问题更多的是关于这个问题是否会隐藏另一个问题?如果我继续向库中添加库标题,我最终还是要给它更多的内存precomp.h吗?这是程序员处理它的方式,还是会有一种"更清洁"的方式呢?

更多信息:

  • Visual Studio 2013
  • C++

c++ precompiled-headers

7
推荐指数
1
解决办法
1565
查看次数

不是类或命名空间名称

我知道这个问题之前已经被问过并回答了,但是没有一个解决方案似乎对我有用,而且我的编译器对这个错误表现得非常奇怪.

当我尝试编译我的代码时,我遇到了许多错误,例如:

Error   1   error C2653: 'TargetList' : is not a class or namespace name    c:\projects\arcturus\augmentedreality\targetlist.cpp    5   1   AugmentedReality
Error   2   error C2065: 'Target' : undeclared identifier   c:\projects\arcturus\augmentedreality\targetlist.cpp    5   1   AugmentedReality
Error   3   error C2146: syntax error : missing ')' before identifier 'target'  c:\projects\arcturus\augmentedreality\targetlist.cpp    5   1   AugmentedReality
Error   4   error C2059: syntax error : ')' c:\projects\arcturus\augmentedreality\targetlist.cpp    5   1   AugmentedReality
Error   5   error C2143: syntax error : missing ';' before '{'  c:\projects\arcturus\augmentedreality\targetlist.cpp    6   1   AugmentedReality
Error   6   error C2447: '{' : …
Run Code Online (Sandbox Code Playgroud)

c++ class precompiled-headers

6
推荐指数
1
解决办法
3万
查看次数

我应该,还是不应该在不同的c文件中包含相同的标题,而这些文件又是主文件中使用的标题?

我正在构建一个main.c文件,以利用几个不同的.h文件中的函数.这些.h文件中的一些(或者更确切地说,它们的.c源文件)使用相同的包含(标准但也包括其他一些)

我的问题是:如果我只在main.c中包含所有头文件,或者我应该让每个.h文件单独包含它们而不是将它们包含在我的main.c中(考虑到我只使用来自那些头文件)?

或者我应该两个都做?

我现在怎么做的是:

dist.c:

#include "dist.h"
#include  <stdio.h>
#include  <unistd.h>
#include  "rpiGpio.h"
#include <pthread.h>
#include  <wiringPi.h>
#include  <softPwm.h>
Run Code Online (Sandbox Code Playgroud)

然后换另一个:

cmps.c:

#include "cmps.h"
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include "rpiGpio.h"
Run Code Online (Sandbox Code Playgroud)

然后在我的main.c:

#include    <stdio.h>
#include    <stdlib.h>
#include    "dist.h"
#include    "cmps.h"
Run Code Online (Sandbox Code Playgroud)

提前致谢!

c precompiled-headers

6
推荐指数
1
解决办法
641
查看次数

如何修复“PCH 警告:标头不在文件范围内停止”

我已经按照我认为正确的方式设置了程序和头文件,但我不断收到标题中提到的错误。

我曾尝试寻找此问题的修复方法,其中大多数只是添加“;” 在头文件中的类定义之后。我已经尝试了几乎所有我能找到的修复方法,但结果相同。

这是标记错误的主程序:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include "computeGross.h"
#include "computeInsurance.h"
#include "Employee.h" /*<-----------------This is where the error flags*/
using namespace std;

int main()
{ }
Run Code Online (Sandbox Code Playgroud)

这是错误标记的头文件:

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;

struct Employee
{
    string name;
    double rate;
    double hours;
    double insurance;
    double social;
    double stateTax;
    double fedTax;
    double netPay;
    double grossPay;
};

#endif
Run Code Online (Sandbox Code Playgroud)

c++ precompiled-headers visual-studio

6
推荐指数
2
解决办法
2万
查看次数