How to add in a CMake project a global file extens

2019-01-25 21:15发布

问题:

I have a very simple CMake script. Unfortunately, the project uses a *.pde file which is plain C++ or C code.

CMake is working with any file ending, but I get a compiler error, because GCC does not know how to handle it. How can I add a global file extension to GCC, so that the *.pde file is compiled as a usual *.cpp file?

The -x c++ foo.pde command is nice if I want to use the console, but for CMake it is (I think) not applicable.

cmake_minimum_required(VERSION 2.8)
project(RPiCopter)

SET( RPiCopter
  absdevice
  containers
  device
  exceptions
  navigation
  frame
  vehicle
  receiver
  scheduler
  tinycopter.pde
)

message( STATUS "Include ArduPilot library directories" )
foreach( DIR ${AP_List} ${AP_List_Linux} ${AP_Headers} )
  include_directories( "../libraries/${DIR}" )
endforeach()
include_directories( zserge-jsmn )

# ***************************************
# Build the firmware
# ***************************************
add_subdirectory ( zserge-jsmn )

#set(CMAKE_CXX_FLAGS "-x c++ *.pde")
ADD_EXECUTABLE ( RPiCopter ${RPiCopter} )
target_link_libraries ( RPiCopter -Wl,--start-group ${AP_List} ${AP_List_Linux} jsmn -Wl,--end-group )

回答1:

You should be able to use set_source_files_properties along with the LANGUAGE property to mark the file(s) as C++ sources:

set_source_files_properties(${TheFiles} PROPERTIES LANGUAGE CXX)

As @steveire pointed out in his own answer, this bug will require something like the following workaround:

set_source_files_properties(${TheFiles} PROPERTIES LANGUAGE CXX)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  add_definitions("-x c++")
endif()


回答2:

I decided to use this approach. I just remove the file ending by cmake in the temporary build directory. So GCC is not confused anymore because of the strange Arduino *.pde file extension.

# Exchange the file ending of the Arduino project file
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tinycopter.pde ${CMAKE_CURRENT_BINARY_DIR}/tinycopter.cpp)


回答3:

Normally you should be able to just extend CMAKE_CXX_SOURCE_FILE_EXTENSIONS. This would help, if you have a lot of files with unknown file extensions.

But this variable is not cached - as e.g. CMAKE_CXX_FLAGS is - so the following code in CMakeCXXCompiler.cmake.in will always overwrite/hide whatever you will set:

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;mm;CPP)

I consider this non-caching being a bug in CMake, but until this is going to be changed I searched for a workaround considering the following:

  • You normally don't want to change files in your CMake's installation
  • It won't have any effect if you change CMAKE_CXX_SOURCE_FILE_EXTENSIONS after project()/enable_language() (as discussed here).

I have successfully tested the following using one of the "hooks"/configuration variables inside CMakeCXXCompiler.cmake.in:

cmake_minimum_required(VERSION 2.8)

set(CMAKE_CXX_SYSROOT_FLAG_CODE "list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS pde)")

project(RPiCopter CXX)

message("CMAKE_CXX_SOURCE_FILE_EXTENSIONS ${CMAKE_CXX_SOURCE_FILE_EXTENSIONS}")

add_executable(RPiCopter tinycopter.pde)


回答4:

CMake doesn't do this for you:

http://public.kitware.com/Bug/view.php?id=14516



标签: gcc cmake