[cmake-developers] [patch] treat .m files as c, not c++

Tim Blechmann tim at klingt.org
Wed Mar 19 12:50:50 EDT 2014


hi all,

compiling .m files with CXX_FLAGS like -std=c++11 causes troubles.
make/ninja treat .m files as c++, which is inconsistent in three
different ways:

a) cmSystemTools::GetFileFormat:
if ( ext == "c" || ext == ".c" ||
       ext == "m" || ext == ".m"
    ) { return cmSystemTools::C_FILE_FORMAT; }
  if (
    ext == "C" || ext == ".C" ||
    ext == "M" || ext == ".M" ||
    ext == "c++" || ext == ".c++" ||
    ext == "cc" || ext == ".cc" ||
    ext == "cpp" || ext == ".cpp" ||
    ext == "cxx" || ext == ".cxx" ||
    ext == "mm" || ext == ".mm"
    )

b) xcode and the cmake/xcode generator

c) the language detection based on the file extension as implemented by gcc:

man gcc:
       file.m
           Objective-C source code.  Note that you must link with the
           libobjc library to make an Objective-C program work.

obj-c is a superset of c, while obj-c++ is a superset of obj-c

this patch corrects this behavior.

best regards,
tim
-------------- next part --------------
>From dc956e47ec280979d62666c42632d01267560927 Mon Sep 17 00:00:00 2001
From: Tim Blechmann <tim at klingt.org>
Date: Wed, 19 Mar 2014 17:31:01 +0100
Subject: [PATCH] Modules: treat .m files as c instead of c++

this gets it in line with cmSystemTools::GetFileFormat. especially
CXX_FLAGS should not be passed to obj-c sources

Signed-off-by: Tim Blechmann <tim at klingt.org>
---
 Modules/CMakeCCompiler.cmake.in   | 2 +-
 Modules/CMakeCXXCompiler.cmake.in | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/Modules/CMakeCCompiler.cmake.in b/Modules/CMakeCCompiler.cmake.in
index 804cce2..694f8b8 100644
--- a/Modules/CMakeCCompiler.cmake.in
+++ b/Modules/CMakeCCompiler.cmake.in
@@ -26,7 +26,7 @@ if(CMAKE_COMPILER_IS_MINGW)
   set(MINGW 1)
 endif()
 set(CMAKE_C_COMPILER_ID_RUN 1)
-set(CMAKE_C_SOURCE_FILE_EXTENSIONS c)
+set(CMAKE_C_SOURCE_FILE_EXTENSIONS c;m)
 set(CMAKE_C_IGNORE_EXTENSIONS h;H;o;O;obj;OBJ;def;DEF;rc;RC)
 set(CMAKE_C_LINKER_PREFERENCE 10)
 
diff --git a/Modules/CMakeCXXCompiler.cmake.in b/Modules/CMakeCXXCompiler.cmake.in
index 35aa6c4..c75611a 100644
--- a/Modules/CMakeCXXCompiler.cmake.in
+++ b/Modules/CMakeCXXCompiler.cmake.in
@@ -27,7 +27,7 @@ if(CMAKE_COMPILER_IS_MINGW)
 endif()
 set(CMAKE_CXX_COMPILER_ID_RUN 1)
 set(CMAKE_CXX_IGNORE_EXTENSIONS inl;h;hpp;HPP;H;o;O;obj;OBJ;def;DEF;rc;RC)
-set(CMAKE_CXX_SOURCE_FILE_EXTENSIONS C;M;c++;cc;cpp;cxx;m;mm;CPP)
+set(CMAKE_CXX_SOURCE_FILE_EXTENSIONS C;M;c++;cc;cpp;cxx;mm;CPP)
 set(CMAKE_CXX_LINKER_PREFERENCE 30)
 set(CMAKE_CXX_LINKER_PREFERENCE_PROPAGATES 1)
 
-- 
1.9.0



More information about the cmake-developers mailing list