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

This is similar to Force CMake for using the C ++ compiler for C files with Visual Studio , but it is not quite the same. This is not the same because its CMake file fails; and I work on almost every modern platform, from BSD and OS X to Solaris and Unix.

I tried to avoid unnecessary checks performed by CMake:

project(cryptopp, CXX) 

This is what happens when I try to generate a 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 do not have C files in our project, so we should be safe with project(cryptopp, CXX) (if I read cmake --help-command project correctly).

This question is about project files, but not CMake files.

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




I am on Ubuntu 12 LTS and it provides:

 $ cmake --version cmake version 2.8.7 
+3
cmake
Jun 20 '16 at 20:14
source share
1 answer

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

So, I have 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 commented, check_type_size() supports the LANGUAGE parameter, but, unfortunately, not for CMake version 2.8.7. But this old version already supports CMAKE_USER_MAKE_RULES_OVERRIDE .

So, I’m still wondering if the best solution for upgrading to the new version of CMake is the best solution (forcing some users of older versions of CMake to update). Or write your own try_compile() snippets.

References

  • How to add a global file extension (* .pde) to a CMake project in GCC, which is processed as C ++ code
  • Change the default value for CMAKE_CXX_FLAGS_DEBUG and friends in CMake
+2
Jun 22 '16 at 20:26
source share



All Articles