Skip to content

PCH source file always built incorrectly as a C++ file #1213

Open

Description

@bdbaddog

This issue was originally created at: 2005-07-14 16:04:41.
This issue was reported by: schooner.

schooner said at 2005-07-14 16:04:41

Consider the Sconstruct with the files associated with the build in the attached zip file. The build is specified with MSVS 7.1 (though the problem occurs even with MSVC 6.0). If you try to build, you will get the following output:

cl /nologo /TP -GX -Ox /c SourcePrecompC.c /FoSourcePrecompC.obj /YcSource.h /FpSourcePrecompC.pch SourcePrecompC.c
cl /nologo -GX -Ox /c Source2.c /FoSource2.obj /YuSource.h /FpSourcePrecompC.pch Source2.c
Source2.c(1) : fatal error C1853: 'SourcePrecompC.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)
scons: *** [Source2.obj] Error 2

The important part to note is "the precompiled header is C++ and you are using it from C" - This happens because when SCons builds SourcePrecompC.c , it adds the /TP flag which instructs the compiler to build the file as a C++ file. However, this needs to be compiled as a c file so that it can be used as the precompiled header for Source2.c, which is a c source file.

The MSVC tool should figure out what flags to pass for the PCH just as it does when it is just a source file.

issues@scons said at 2005-07-14 16:04:41

Converted from SourceForge tracker item 1238542

gregnoel said at 2008-10-05 07:54:06

Bug party triage. Gary to research and see if it's still a problem.

garyo said at 2008-10-05 18:46:42

I'd like to look into this bug, but it seems like the attachment is missing. It might have been lost when we converted to Tigris. Schooner, if you're still around (I know this was a long time ago!) could you repost the attachment? If I don't hear anything in a week, I'll have to close the bug due to lack of ability to reproduce it.

garyo said at 2008-10-19 06:28:16

Resolving as invalid due to missing attachment & no reply from OP, sorry.

Activity

  1. added this to the milestone on Jan 2, 2018
  2. mwichmann commented on Nov 5, 2023

    @mwichmann
    Collaborator

    This bug got closed for reproducibility reasons, but it's not hard to conclude the report hits on something significant. The compiler itself has reported a problem as:

    the precompiled header is C++ and you are using it from C (or vice versa)

    Showing us they can't be mixed, and the current implementation (SCons/Tool/msvc.py) sets the compiler for a PCH build unconditionally to:

        env['PCHCOM'] = '$CXX /Fo${TARGETS[1]} $CXXFLAGS $CCFLAGS $CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS /c $SOURCES /Yc$PCHSTOP /Fp${TARGETS[0]} $CCPDBFLAGS $PCHPDBFLAGS'

    $CXX evaluates to the same as $CC so that's not a problem, but $CXXFLAGS contains /TP, which means C++, as noted in the original report. From the Microsoft docs:

    /TC and /TP are global variants of /Tc and /Tp. They specify to the compiler to treat all files named on the command line as C source files (/TC) or C++ source files (/TP), without regard to location on the command line in relation to the option.

    So we don't really have a mechanism in place to distinguish between C and C++, we just tell the compiler to assume it's all C++. Which usually isn't an issue, since C is part of the C++ standard, so they're essentially compatible, but in the case of PCH files, Microsoft has chosen to make a distinction.

    Temporarily, I'm reopening this - it's possible the resolution is "tough, we're not going to go down this rathole, Don't Do That" (which might imply a small doc note somewhere).

  3. added
    MSVC
    pch
    and removed on Nov 5, 2023
  4. mwichmann commented on Nov 13, 2023

    @mwichmann
    Collaborator

    PR #4441 proposes wording to at least document this assumption ("PCH is always C++"). It's done as a note so easy to update if there's ever a resolution to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    MSVCVersion: 0.96.90bugpch

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions