Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

gcc

cmake

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 )
like image 204
dgrat Avatar asked May 31 '15 10:05

dgrat


People also ask

What is CMake and GCC?

CMake generates files for other build systems. These can be Makefiles, Ninja files or projects files for IDEs like Visual Studio or Eclipse. The build files contain calls to compilers like GCC, Clang, or cl.exe. If you have several compilers installed, you can choose one. All three parts are independent.

Can you use CMake with C?

In the C/C++ ecosystem, the best tool for project configuration is CMake. CMake allows you to specify the build of a project, in files named CMakeLists. txt, with a simple syntax (much simpler than writing Makefiles).

What is CMake file extension?

A CMAKE file is a CMake language source file used by CMake, a collection of tools designed to build, test, and package software. The file may contain a script or module written in the CMake language. CMake Language source files are organized into "Directories" (CMakeLists. txt), "Scripts" (script.


2 Answers

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()
like image 185
Fraser Avatar answered Sep 23 '22 02:09

Fraser


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)
like image 37
dgrat Avatar answered Sep 25 '22 02:09

dgrat