How to add in a CMake project a global file extension (*.pde) to GCC which is treated like C++ code

后端 未结 4 645
陌清茗
陌清茗 2020-12-18 03:40

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 comp

相关标签:
4条回答
  • 2020-12-18 03:53

    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()
    
    0 讨论(0)
  • 2020-12-18 03:56

    CMake doesn't do this for you:

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

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-18 04:01

    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)
    
    0 讨论(0)
提交回复
热议问题