告诉 CMake 对来自 CMake 的 C 文件使用 C++ 编译器? [英] Tell CMake to use C++ compiler for C files coming from CMake?

查看:36
本文介绍了告诉 CMake 对来自 CMake 的 C 文件使用 C++ 编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这类似于强制 CMake 对带有 Visual Studio 的 C 文件使用 C++ 编译器,但并不完全相同.它不一样,因为它的 CMake 文件导致失败;我在几乎所有现代平台上工作,从 BSD 和 OS X 到 Solaris 和 Unix.

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.

我试图避免 CMake 执行无用的检查:

I tried to avoid useless checks being performed by CMake:

project(cryptopp, CXX)

以下是我尝试生成 makefile 时发生的情况:

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)

我们的项目中没有任何 C 文件,因此使用 project(cryptopp, CXX) 应该是安全的(如果我正在阅读 cmake --help-command project 正确).

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).

引用的问题涉及项目文件,但不涉及 CMake 文件.

The cited question talks about project files, but not CMake files.

如何告诉 CMake 对所有文件使用 C++ 编译器,包括它自己的 CMake 文件?

How do I tell CMake to use C++ compiler for all files, including its own CMake files?

我使用的是 Ubuntu 12 LTS,它提供:

I'm on Ubuntu 12 LTS, and it provides:

$ cmake --version
cmake version 2.8.7

推荐答案

有多种方法可以将 .c 添加为 CXX 编译器的有效文件扩展名.即使这是非常高级的 CMake 东西,您也可能需要 - 如果您必须支持旧版本的 CMake - 无论如何都需要制作规则覆盖脚本".

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)

正如@Tsyvarev 所评论的check_type_size() 支持 LANGUAGE 参数,但遗憾的是 CMake 版本 2.8.7 不支持.但是这个旧版本已经支持 CMAKE_USER_MAKE_RULES_OVERRIDE.

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.

所以我仍然想知道是否最好的解决方案是使用较新版本的 CMake(迫使一些旧版本的 CMake 用户升级).或者编写自己的 try_compile() 片段.

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.

参考资料

这篇关于告诉 CMake 对来自 CMake 的 C 文件使用 C++ 编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆