嘿,我过去几天一直在关注learncpp.com tuts,他们说要从.cpp文件中注释掉"#include"stdafx.h"Code :: Blocks.
这是必须的,删除包含线?如果您有数百个文件并从Win7上的Visual Studio更改为Linux上的Code :: Blocks,或者使用mac将其交给其他人,会发生什么?
c++ cross-platform precompiled-headers stdafx.h visual-studio
这些函数是我的大多数程序对象将使用的实用程序类型的东西.我希望将它们放在命名空间中并使它们具有全局性.此命名空间在标头中定义,然后添加到我的预编译标头中.但是到目前为止,我已经在2个不同的对象中使用了这个命名空间中的函数,并且编译器在这两个对象上抛出了多次定义的符号错误.
命名空间文件
#ifndef UTILS_H
#define UTILS_H
#include <random>
#include <cmath>
namespace Utils
{
extern int GetRandomBetween(int low, int high)
{
if (low < 0 || low >= high)
return 0;
int seed = high - low;
return (rand() % seed) + low;
}
};
#endif
Run Code Online (Sandbox Code Playgroud)
和我的precomp标题
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
//#define WIN32_LEAN_AND_MEAN // Exclude …Run Code Online (Sandbox Code Playgroud) 我知道这个问题之前已经被问过并回答了,但是没有一个解决方案似乎对我有用,而且我的编译器对这个错误表现得非常奇怪.
当我尝试编译我的代码时,我遇到了许多错误,例如:
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) 我在Visual Studio中有近200个项目,构建它们需要很长时间.我注意到为每个项目构建stdafx.cpp(预编译头)很慢.我为每个项目使用相同的标题,为什么我要构建它〜200次?如何构建它并在项目之间共享?
我正在构建一个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)
提前致谢!
我的项目有时会受益于ccache,所以我一直在使用ccache.我现在正在添加预编译头文件.一些消息来源表明,这两者是不相容的,必须在它们之间做出选择.但我在ccache的文档中发现它在某种程度上支持PCH:https: //ccache.samba.org/manual.html#_precompiled_headers
实际上,当我尝试使用ccache在使用Clang -include-pch选项的同时构建.o文件时,我发现ccache正在成功缓存.o.第一次编译尝试需要1.5秒,第二次只需0.05秒(因为ccache完成了它的工作).
麻烦的是,如果我用clang++而不是with 运行相同的编译命令/usr/lib/ccache/clang++,则需要0.5秒......除非我离开-include-pch部分,在这种情况下需要大约1.5秒.似乎ccache可能会导致我的PCH被忽略,或者其他什么.
我按照说明(从上面的链接).正如那里指出的那样,我的ccache.conf看起来像这样:
sloppiness=pch_defines,time_macros
Run Code Online (Sandbox Code Playgroud)
我已经试过的一切合理的组合#include,-include,-include-pch和-fpch-preprocess我能想到的.编译总是花费1.5秒然后0.05秒,当它应该花费0.5秒,然后0.05秒.
有可能做到这一点,或者我必须在ccache和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) 我正在开发一个必须在多种环境下构建的大型项目,主要是linux/gcc和windows/msvc.为了加快构建速度,我们使用预编译头文件.
Windows实现非常高效:在我的四核超线程i7构建时间从9分钟下降到1.5分钟.但是,使用预编译的头文件似乎并没有提高性能:在这两种情况下,它在同一台计算机上的虚拟盒下构建22分钟,或者在真实服务器上构建大约40分钟.
所以我想的是显而易见的,我在某种程度上出了问题并且预编译的头文件没有被踢进去.但是我无法找到它.
我们的Makefile是由CMake生成的,所以我可以复制粘贴用于编译头的代码和使用它们的目标文件.
创建标题:
/usr/bin/c++ -O3 -DNDEBUG --no-warnings "-I/mnt/code/server a/src/game"
"-I/mnt/code/server a/src/game/vmap" "-I/mnt/code/server a/dep/include/g3dlite"
"-I/mnt/code/server a/dep/include" "-I/mnt/code/server a/src/shared"
"-I/mnt/code/server a/src/framework" "-I/mnt/code/server a/buildlinux"
"-I/mnt/code/server a/buildlinux/src/shared" -I/usr/include/mysql
"-I/mnt/code/server a/dep/acelite" -DDO_MYSQL -DHAVE_CONFIG_H
-DVERSION=\"0.6.1\" -DSYSCONFDIR=\"../etc/\" -D_RELEASE -D_NDEBUG -x c++-header
-o "/mnt/code/server a/buildlinux/src/game/pchdef.h.gch" "/mnt/code/server
a/src/game/pchdef.h"
Run Code Online (Sandbox Code Playgroud)
编译目标文件:
/usr/bin/c++ $(CXX_DEFINES) $(CXX_FLAGS) "-I/mnt/code/server
a/buildlinux/src/game" -include pchdef.h -Winvalid-pch -o
CMakeFiles/game.dir/AccountMgr.cpp.o -c "/mnt/code/server
a/src/game/AccountMgr.cpp"
Run Code Online (Sandbox Code Playgroud)
即使它们不是直接来自上面的片段,也可以理解见解.
我最近发现了有关预编译头文件C++,特别是gcc编译器.我确实.h.gch在网上找到了一些关于文件的东西,但还没有能够使用它们.
我需要知道:
.h.gch文件?我的代码没有创建一个?#include "SomeCode.h.gch"或者还有其他一些使用它的方法吗?请帮忙
我想要做的是设置一个项目,如果设置了预编译标头,则无关紧要.stdafx.hpp是我的PH
我的主要问题是我有多个目录上的文件,而不仅仅是项目根目录.我尝试了什么:
如果我
#include "stdafx.hpp"
Run Code Online (Sandbox Code Playgroud)
PH设置无处不在,但如果我将其停用,它会抱怨,因为子目录中的文件无法找到它.
如果我
#include "../stdafx.hpp"
Run Code Online (Sandbox Code Playgroud)
在子目录中的文件中,它在没有PH的情况下工作正常,但是对于PH,编译器抱怨stdafx.hpp没有包含它.
如果我stdafx.hpp使用相对或绝对路径设置强制包含文件,编译器给了我错误(现在不能记住,如果有必要我将重现它).
那么,可能是什么解决方案呢?