告诉CMake使用C ++编译器的C文件来自CMake? [英] Tell CMake to use C++ compiler for C files coming from CMake?
问题描述
这类似于强制CMake使用C ++编译器将C文件与Visual Studio ,但它不完全相同。它不一样,因为它的CMake文件导致失败;我在几乎所有的现代平台上工作,从BSD和OS X到Solaris和Unix。
我试图避免CMake执行无用的检查:
项目(cryptopp,CXX)
b $ b
下面是当我尝试生成makefile时会发生什么:
$ cmake。
- 检查系统是否是大端
- 搜索16位整数
- 检查无符号short的大小
CMake错误在/usr/share/cmake-2.8/ Modules / CheckTypeSize.cmake:82(try_compile):
文件的未知扩展名.c
/home/jeffrey/cryptopp/CMakeFiles/CheckTypeSize/CMAKE_SIZEOF_UNSIGNED_SHORT.c
try_compile()仅适用于已启用的语言。目前这些是:
CXX
请参阅project()命令以启用其他语言。
调用堆栈(最近调用):
/usr/share/cmake-2.8/Modules/CheckTypeSize.cmake:167(__check_type_size_impl)
/usr/share/cmake-2.8/Modules /TestBigEndian.cmake:27(CHECK_TYPE_SIZE)
CMakeLists.txt:49(TEST_BIG_ENDIAN)
我们没有任何C文件在我们的项目,所以我们应该是安全的项目(cryptopp,CXX)
(如果我正在阅读 cmake - -help-command project
)。
引用的问题涉及项目文件,而不是CMake文件。
如何告诉CMake为所有文件使用C ++编译器,包括它自己的CMake文件?
我在Ubuntu 12 LTS上,它提供:
$ cmake --version
2.8.7
$ c> .c 作为 CXX
编译器的有效文件扩展名。即使这是非常先进的CMake东西,你可能需要 - 如果你必须支持旧版本的CMake - 无论如何制定规则覆盖脚本。
所以我已经成功测试了以下内容:
CryptoppMakeRulesOverwrite.cmake / p>
列表(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS c)
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.7 FATAL_ERROR)
set(CMAKE_USER_MAKE_RULES_OVERRIDECryptoppMakeRulesOverwrite.cmake)
项目(cryptopp,CXX)
include(CheckTypeSize)
CHECK_TYPE_SIZE unsigned shortCMAKE_SIZEOF_UNSIGNED_SHORT)
由于@Tsyvarev已注释 check_type_size()
支持 LANGUAGE
参数,但不幸的是没有CMake版本2.8.7。但此旧版本已支持 CMAKE_USER_MAKE_RULES_OVERRIDE
。
所以我仍然想知道,如果不是最好的解决方案是去一个较新的版本的CMake(强迫一些旧的CMake版本的用户升级)。或撰写您自己的 try_compile()
的片段。
参考 b $ b
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; and I'm working on nearly every modern platform, from BSDs and OS X through Solaris and Unix.
I tried to avoid useless checks being performed by CMake:
project(cryptopp, CXX)
Here's what happens when I attempt to generate the makefile:
$ cmake .
-- Check if the system is big endian
-- Searching 16 bit integer
-- Check size of unsigned short
CMake Error at /usr/share/cmake-2.8/Modules/CheckTypeSize.cmake:82 (try_compile):
Unknown extension ".c" for file
/home/jeffrey/cryptopp/CMakeFiles/CheckTypeSize/CMAKE_SIZEOF_UNSIGNED_SHORT.c
try_compile() works only for enabled languages. Currently these are:
CXX
See project() command to enable other languages.
Call Stack (most recent call first):
/usr/share/cmake-2.8/Modules/CheckTypeSize.cmake:167 (__check_type_size_impl)
/usr/share/cmake-2.8/Modules/TestBigEndian.cmake:27 (CHECK_TYPE_SIZE)
CMakeLists.txt:49 (TEST_BIG_ENDIAN)
We don't have any C files in our project, so we should be safe with project(cryptopp, CXX)
(if I am reading cmake --help-command project
correctly).
The cited question talks about project files, but not CMake files.
How do I tell CMake to use C++ compiler for all files, including its own CMake files?
I'm on Ubuntu 12 LTS, and it provides:
$ cmake --version
cmake version 2.8.7
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
这篇关于告诉CMake使用C ++编译器的C文件来自CMake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!