Search code examples
c++cxc16

Building foo.cpp as C code with Microchip XC16 compiler


I have some code that is shared between a C++ Windows application and an XC16 C application. The source is written in such a way that it compiles cleanly under MSVC and XC16 (with appropriate use of the __cplusplus macro).

The problem is that XC16 refuses to build foo.cpp. I have to copy the file to foo.c and back again. This is wreaking havoc on revision control, and making code edits within the ide problematic when the copying functions are automated in the build process.

Is there a way to get XC16 to build (as C) a file with the extension cpp? (note that the header file is foo.h, so it is not an issue).


Solution

  • We discovered a flag in one of our Windows build environments (Embarcadero CBuilder) that forces compiling foo.c as C++. We'll therefore rename to foo.c. Problem solved. (EDIT: Others have commented that similar flags exist in VC)

    We could not find a comparable flag in XC16 (based on gcc 4.5.1) to treat foo.cpp as a C source file. The -x flag (Forcing gcc to compile .cpp file as C) does not seem to work under MPLABX.