Copyright ©

Mongoose OS Forum

ATTENTION! This forum has moved to:

Do not post any new messages.

Use C++ classes?

Hi, I'm just getting started with Mongoose OS, and would like to write my code in C++ (not C), so that I can use classes.

I tried to do a simple test here, where I make a sum.hpp and a sum.cpp, and add an include in main.c to include the sum.hpp file.

The sum.hpp looks like this:

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

class Calculator {
    int sum(int a, int b);

#ifdef __cplusplus
#endif /* __cplusplus */

And my sum.cpp looks like this:

#include "sum.hpp"

int Calculator::sum(int a, int b) {
    return a + b;


When I do a 'mos build' it fails saying that:
/src/sum.hpp:6:1: error: unknown type name 'class'
class Calculator {

Does anyone know how to get C++ classes working in Mongoose OS?


  • Looks like you're including your sum.hpp not only in sum.cpp, but also in some C file (maybe main.c?). C files are handled by C compiler which don't know how to compile C++. Btw you can rename main.c to main.cpp and write C++ there.

    Thanked by 1vipulgarg
  • Hi, you are right about the reference to sum.hpp in main.c.

    However, I've renamed main.c to main.cpp, and it won't compile.

    In order to rule out any typos, etc, I started a new project. Here is a step by step guide to what I did:

    1. Cloned
    2. Ran 'mos build', and it compiled successfully
    3. Renamed main.c to main.cpp
    4. Ran 'mos build' again, but this time it fails to compile. There is a whooole lot of output text, and I can't spot one specific error-line, except maybe this one:

    make[1]: *** No rule to make target '/fwbuild-volumes/latest/apps/TestCPP/esp32/build_contexts/build_ctx_241293700/src/main.c', needed by 'main.o'. Stop.

    Anything else I need to do to compile C++ via 'mos build'?

  • You need a clean build: add --clean to your mos build command.

  • Thanks, that was the reason!

Sign In or Register to comment.