Using pre-compiled headers with CMake

后端 未结 14 2154
刺人心
刺人心 2020-12-12 10:39

I have seen a few (old) posts on the \'net about hacking together some support for pre-compiled headers in CMake. They all seem a bit all-over the place and everyone has the

相关标签:
14条回答
  • 2020-12-12 11:08

    CMake has just gained support for PCHs, it should be available in the upcoming 3.16 release, due 2019-10-01:

    https://gitlab.kitware.com/cmake/cmake/merge_requests/3553

      target_precompile_headers(<target>
        <INTERFACE|PUBLIC|PRIVATE> [header1...]
        [<INTERFACE|PUBLIC|PRIVATE> [header2...] ...])
    

    There is ongoing discussion on supporting sharing PCHs between targets: https://gitlab.kitware.com/cmake/cmake/issues/19659

    There is some additional context (motivation, numbers) available at https://blog.qt.io/blog/2019/08/01/precompiled-headers-and-unity-jumbo-builds-in-upcoming-cmake/

    0 讨论(0)
  • 2020-12-12 11:13

    Adapted from Dave, but more efficient (sets target properties, not for each file):

    if (MSVC)
       set_target_properties(abc PROPERTIES COMPILE_FLAGS "/Yustd.h")
       set_source_files_properties(std.cpp PROPERTIES COMPILE_FLAGS "/Ycstd.h")
    endif(MSVC)
    
    0 讨论(0)
  • 2020-12-12 11:13

    CMake 3.16 introduced support for precompiled headers. There is a new CMake command target_precompile_headers which does everything you need under the hood. See its documentation for further details: https://cmake.org/cmake/help/latest/command/target_precompile_headers.html

    0 讨论(0)
  • 2020-12-12 11:16

    There is a third party CMake module named 'Cotire' which automates the use of precompiled headers for CMake based build systems and also supports unity builds.

    0 讨论(0)
  • 2020-12-12 11:17

    An example of usage precompiled header with cmake and Visual Studio 2015

    "stdafx.h", "stdafx.cpp" - precompiled header name.

    Put the following below in the root cmake file.

    if (MSVC)
        # For precompiled header.
        # Set 
        # "Precompiled Header" to "Use (/Yu)"
        # "Precompiled Header File" to "stdafx.h"
        set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Yustdafx.h /FIstdafx.h")
    endif()
    

    Put the following below in the project cmake file.

    "src" - a folder with source files.

    set_source_files_properties(src/stdafx.cpp
        PROPERTIES
        COMPILE_FLAGS "/Ycstdafx.h"
    )
    
    0 讨论(0)
  • 2020-12-12 11:18

    The cleanest way is to add the precompiled option as a global option. In the vcxproj file this will show up as <PrecompiledHeader>Use</PrecompiledHeader> and not do this for every individual file.

    Then you need to add the Create option to the StdAfx.cpp. The following is how I use it:

    MACRO(ADD_MSVC_PRECOMPILED_HEADER SourcesVar)
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /YuStdAfx.h")
        set_source_files_properties(StdAfx.cpp
            PROPERTIES
            COMPILE_FLAGS "/YcStdAfx.h"
            )
        list(APPEND ${${SourcesVar}} StdAfx.cpp)
    ENDMACRO(ADD_MSVC_PRECOMPILED_HEADER)
    
    file(GLOB_RECURSE MYDLL_SRC
        "*.h"
        "*.cpp"
        "*.rc")
    
    ADD_MSVC_PRECOMPILED_HEADER(MYDLL_SRC)
    add_library(MyDll SHARED ${MYDLL_SRC})
    

    This is tested and works for MSVC 2010 and will create a MyDll.pch file, I am not bothered what file name is used so I didn't make any effort to specify it.

    0 讨论(0)
提交回复
热议问题