混合 C 和 C++ 的预编译头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8934354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Precompiled Headers with Mixed C and C++
提问by James Linden
I am using pre-compiled headers in my project in C but we are integrating a .CPP file into the project.
我在我的 C 项目中使用预编译头文件,但我们正在将 .CPP 文件集成到项目中。
Here's the error:
这是错误:
Error 1 fatal error C1853: 'Debug\MuffinFactory.pch' precompiled header
file is from a previous version of the compiler, or the precompiled header is C++
and you are using it from C (or vice versa)
c:\users\blake\desktop\projects\muffinfactory\source\main.cpp 1
We only need a single .CPP compiled in our project, but we really need the pre-compiled header to save compile times (Windows.h and more).
我们只需要在我们的项目中编译一个 .CPP,但我们确实需要预编译的头文件来节省编译时间(Windows.h 和更多)。
How should I organize my project to do this?
我应该如何组织我的项目来做到这一点?
采纳答案by Bo Persson
So don't use precompiled headers for that single file!
所以不要为那个单个文件使用预编译的头文件!
Being a .cpp file, it will have separate compilation options anyway.
作为一个 .cpp 文件,它无论如何都会有单独的编译选项。
回答by Mark Ransom
You might be able to create two precompiled headers in your project. There's a property on each source file that determines if it's going to use a precompiled header, or generate a precompiled header - try setting two different sources to generate a header.
您可以在项目中创建两个预编译头文件。每个源文件都有一个属性,用于确定它是要使用预编译头还是生成预编译头 - 尝试设置两个不同的源来生成头。
回答by zdan
Try creating a separate C++ precompiled header file (say MuffinFactoryCpp.h which is a copy of the other one). Look at the project settings under "Precompiled Headers" and use this new header file as the precompiled header for the C++ source file.
尝试创建一个单独的 C++ 预编译头文件(比如 MuffinFactoryCpp.h,它是另一个文件的副本)。查看“预编译头”下的项目设置,并使用这个新头文件作为 C++ 源文件的预编译头。
回答by Ivan
Zuuum's Answer
祖姆的回答
Apologies to Zuuumfor such a blatant rip off of his answer, but 7 years later it is still buried as a comment. Bo Perrsontells us what to do
向Zuuum道歉,因为他的回答如此明目张胆,但 7 年后它仍然作为评论被埋葬。Bo Perrson告诉我们该怎么做
So don't use precompiled headers for that single file!
It will have separate compilation options anyway.
所以不要为那个单个文件使用预编译的头文件!
无论如何,它将有单独的编译选项。
and Zuuum tells us how
Zuuum 告诉我们如何
Extra Info: Select the file you don't want to use "Precompiled Header" from the Solution Explorer
Right Click
Under Precompiled Headers Option, Select Not Use Precompiled Header
– Zuuum Feb 19 '12 at 18:34
额外信息:从解决方案资源管理器中选择您不想使用“预编译头”的文件在预编译头选项下
右键单击
,选择不使用预编译头
– Zuuum 2012 年 2 月 19 日 18:34
I've made tiny edits - they are not direct quotes. The reason for my change is you may want to exclude a C or a C++ file from using precompiled headers for a particular case. Bo assumes C++ as it's a direct answer to the question. Zuuum assumes C, and that's the case for me and in my illustration. It could be either in practice.
我做了一些微小的编辑——它们不是直接引用。我更改的原因是您可能希望在特定情况下从使用预编译头文件中排除 C 或 C++ 文件。Bo 假设 C++ 是对问题的直接回答。Zuuum 假设 C,这就是我和我的插图的情况。它可以是在实践中。
It's understandable but annoying that the location of options in menus changes from one release of visual studio to another, but here is the location of the menu item in my current visual studio (2019).
菜单中选项的位置从 Visual Studio 的一个版本更改为另一个版本,这是可以理解但令人讨厌的,但这是我当前 Visual Studio (2019) 中菜单项的位置。
回答by Brandon Nolet
I just looked up the error here and found this thread. However, upon trial and error, I found that the issue was that I did not have all files saved recently. It seems that Visual Studio is a little finicky about save dates of files that are attached together.
我只是在这里查找错误并找到了这个线程。但是,经过反复试验,我发现问题是我最近没有保存所有文件。Visual Studio 似乎对附加在一起的文件的保存日期有点挑剔。
I went around hitting Ctrl+S on all the source files and that fixed the issue.
我在所有源文件上按 Ctrl+S 并解决了问题。