[CMake] CMake precompiled header support (in MSVC 7.1)

Alexandru Ciobanu aciobanu at matrox.com
Wed Jul 18 12:10:08 EDT 2007


Hi, Alexander!

I've been able to implement Precompiled Headers for a project. We use 
the Intel compiler on Linux, but still, the steps are the same:
   - compile a stdafx.cpp with /Yc ( gives the .pch file )
   - compile the normal source files using /Yu

Here is a rough sketch of a possible solution:

    # in ${src} you have all the source files

    if ( ${PCH} STREQUAL "ON" )

        # here you build the pch ( force it as a static lib )
        add_compile_flags ( stdafx.cpp "/Ycstdafx.pch" )
        add_library ( pch_stdafx STATIC stdafx.cpp )

        foreach ( i ${src} )
            add_compile_flags ( ${i} "/Yustdafx.pch" )
        endforeach ( i ${src} )

    endif ( ${PCH} STREQUAL "ON" )

    # here you add your lib/exe
    add_libraray ( yourLIB SHARED ${src} )

    if ( ${PCH} STREQUAL "ON" )
       add_dependencies ( yourLIB pch_stdafx )
    endif ( ${PCH} STREQUAL "ON" )


NOTE: add_compile_flags () is macro to append compile flags to files unlike
set_source_files_properties() which overwrites them.

Since CMake does not have an add_object() command in order to use it for 
building
stdafx.cpp, I have used something like add_libraray ( pch_target STATIC 
stdafx.cpp )
to do it. You will get the extra lib (libpch_stdafx.a), but you'll also 
get the .pch file.

Setting pch_stdafx as dependency to yourLIB is important. *This* is what
makes your .pch file be created *before* the sources are compiled.

Please also note that I did this for Intel C++ compiler.  I hope you can
implement this in MSVC as well.

Alex Ciobanu




Alexander Jasper wrote:
> Hi all,
>
> First of all I like say thank you to the Cmake community for providing this great build tool free of charge. 
>
> After using it for some while now we are facing problems with the build times. Therefore we think using pre-compiled headers in MSVC 7.1. As CMake doesn’t seem to have "native" support precompiled headers we added the corresponding options to the compiler options by at appropriate Cmake command. Apart from being platform depdent this also has the drawback that MSVC does honor the build ordr that is required when sing PCH. When using PCH “through a file” (usually stdafx.h in MSVC projects) you have a corresponding .ccp file building the precompiled headers while being build itself. Hence, this .ccp has to be the first being built for each project. MSVC takes care of that if indicate what PCH to use in the project settings. But what we are doing is to provide the corresponding options to the compiler directly. The MSVC in this does take care of building the PCH first. This results in an error.
>
> Does anybody know a solution here or have a suggestion or hint?
> W/o PCH support building large projects is very time consuming, so I‘m sure there must be solution or were I missing something the manual? Surprisingly  I did not find much information regarding that on the web.
>
> Thanks in advance.
>
> Kind regards
>
> Alexander
>   




More information about the CMake mailing list