Tell CMake to use C++ compiler for C files coming from CMake?

前端 未结 1 1911
灰色年华
灰色年华 2020-11-29 11:50

This is similar to Force CMake to use C++ compiler for C files with Visual Studio, but its not quite the same. Its not the same because a its CMake file causing the failure;

相关标签:
1条回答
  • 2020-11-29 12:25

    There are ways to add .c as a valid file extension for the CXX compiler. Even this being very advanced CMake stuff, you may need - if you are bound to support older versions of CMake - a "make rules overwrite script" anyway.

    So I've successfully tested the following:

    CryptoppMakeRulesOverwrite.cmake

    list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS c)
    

    CMakeLists.txt

    cmake_minimum_required(VERSION 2.8.7 FATAL_ERROR)
    
    set(CMAKE_USER_MAKE_RULES_OVERRIDE "CryptoppMakeRulesOverwrite.cmake")
    
    project(cryptopp CXX)
    
    include(CheckTypeSize)
    CHECK_TYPE_SIZE("unsigned short" CMAKE_SIZEOF_UNSIGNED_SHORT)
    

    As @Tsyvarev has commented check_type_size() supports a LANGUAGE parameter, but unfortunately not for CMake version 2.8.7. But this older version does already support CMAKE_USER_MAKE_RULES_OVERRIDE.

    So I'm still wondering if not the best solution would be to go to a newer version of CMake (forcing some users of older CMake versions to upgrade). Or writing your own try_compile() snippets.

    References

    • How to add in a CMake project a global file extension (*.pde) to GCC which is treated like C++ code
    • Change default value of CMAKE_CXX_FLAGS_DEBUG and friends in CMake
    0 讨论(0)
提交回复
热议问题