0

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).

EBlake
  • 735
  • 7
  • 14
  • 5
    Why don’t you compile it as C on MSVC? –  Feb 07 '18 at 21:02
  • How are you building for Windows? VisualStudio?, MSBuild? – Mark Benningfield Feb 08 '18 at 00:20
  • In the Windows environment, the code is encapsulated in a class (so it needs to be built with C++). This is not possible when compiled as C with a 16-bit microprocessor target. – EBlake Feb 08 '18 at 00:29
  • 1
    Don't know about XC16, but MSVC has options to build as C or C++ with any extension. [Specify Source File Type](https://msdn.microsoft.com/en-us/library/032xwy55.aspx) – Bo Persson Feb 08 '18 at 00:32
  • @BoPersson Thanks. We found a similar solution (see my answer). Unfortunately this does not seem to be an option for XC16. More specifically, I think the problem lies with MPLABX, which dynamically builds the makefile. – EBlake Feb 08 '18 at 00:37
  • You should compile C code as C in both cases; in the C++ app you could have a wrapper that wraps the C code without requiring modification of the C code – M.M Feb 08 '18 at 00:48

1 Answers1

0

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.

EBlake
  • 735
  • 7
  • 14