C + + Mixed programming occurs: The precompiled header file comes from an earlier version of the compiler, or the precompiled header is C + + and uses it (or vice versa) in C to solve the problem

Source: Internet
Author: User

Today, I tried a C/C + + mixed programming, and then there was the problem of the egg ache above, went to the internet to check, found that the original is the language and the pre-C + + precompiled header problems.


Error: The precompiled header file comes from an earlier version of the compiler, or the precompiled header is C + + and uses it in C (or vice versa)

English version: fatal error C1853: ' pjtname.pch ' precompiled header file is from a previous version of the compiler, or the Precompile D header is C + + and you have using it from C (or vice versa)

or fatal error C1853: ' xxx.pch ' is not a precompiled header file created with this compiler.


The problem is that when there are both. C and. cpp files in the VS project, the compiler will compile them differently (mainly because the function declaration is handled differently, the C language is not polymorphic, the function names are compiled more normally, and C + + has polymorphic properties, so the function name is unrecognizable after compilation).


So what's the precompiled header? The following copy from Baidu Encyclopedia:

The so-called precompiled header, is the head file in advance compiled into a binary intermediate format. The precompiled header is physically the same as the usual. obj file, but do not confuse the intermediate format with the. o/.obj/.a/.lib format, they are completely different! So the attributes and target files of the precompiled header file are different (although they all belong to some intermediate file). The. h,.c,.cpp files that are compiled into the precompiled header are compiled only once throughout the compilation process, such as the parts involved in the precompiled header do not change, and this part is not recompiled during the subsequent compilation. This greatly increases the speed of compilation and facilitates the management of the header files, and also helps to eliminate the duplication of inclusion problems. --but there are similar places, for example, they are incompatible between the compiler ^_^, that is, you can not get the VC generated by the precompiled head to the GCC. Even the extension is not the same, VC is familiar to everyone. PCH, and GCC's, is. GCH.

So when do you want to use the precompiled header? The following is still copied from the Baidu Encyclopedia:

1) when most. C or. cpp files require the same header file.
2) When some code is reused heavily.
3) When importing some of the different libraries have implemented functions and generate confusion.


So, the problem is mainly because C and C + + precompiled header, vs default to use C + + precompiled header, and C language requires other types of precompiled header, can not take into two words.


Three solutions:

1. The simplest and most brutal approach is that the entire project does not use a precompiled header. It is strongly deprecated, however, that precompiled headers can greatly reduce compilation time, so it is important to retain precompiled headers.

2. Look at the C language or C + + file More, the less the kind of file of the precompiled head removed. This is relatively simple.

3. Set the C file as a whole project, compile it into DLL or lib dynamic load, the whole project uses the same precompiled header.


With respect to the precompiled header, there are two options, one is Yu (using precompiled header), one is the YC (Create Precompiled header), the Stdafx.cpp option is YC, that is, the precompiled header is created, and the properties of the other files are Yu.


about how to modify the precompiled header settings:


Since there is only one C file, you can modify the properties of the. c file directly to not use the precompiled header.


A note on C/C + + mixed programming should be noted that C + + is required to use the function of the language, or to reference the C language header file, you need to precede the extern "C" keyword.

For example:

The. h file of the C language

#ifndef __cfile_h_#define __cfile_h_extern int ctest (int a, int b); #endif
. c Files
#include "CFile.h" int ctest (int a, int b) {return a + B;}

CPP file:

#include "stdafx.h" #include <iostream>using namespace Std;extern "C" {#include "CFile.h"};int _tmain (int argc, _ tchar* argv[]) {int a = 1;int B = 2;cout<<ctest (A, b); system ("pause"); return 0;}

















Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Mixed programming occurs: The precompiled header file comes from an earlier version of the compiler, or the precompiled header is C + + and uses it (or vice versa) in C to solve the problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.