Skip to main content
Explorer
October 7, 2024
Question

C++ project but unable to use C++ code

  • October 7, 2024
  • 2 replies
  • 1031 views

Hello, I have created a C++ project with CubeMx / CubeIDE.  It generates the code as main.c, I have seen other post about changing the file extension each time you generate code to .cpp and I have tried that but then I get an "undefined reference to `MX_FREERTOS_Init()" error.  I can then change app_freertos to a .cpp file but that creates more errors.

If I leave the file extensions of main and app_freertos as .c everything will compile and run but I then run into an issue of including a library I wrote in C++ that uses a class.  I have a class defined in my header file but it wont compile..."unknown type name 'class'".

    This topic has been closed for replies.

    2 replies

    Graduate II
    October 8, 2024

    You are trying to use the keyword "class" in a .c file, that will not work because class is not part of the C language. If you rename your .c file to .cpp and update your project accordingly you should be able to then use C++ features in your code.

    Super User
    October 8, 2024

    Generally, the best way to incorporate C++ into your project is to leave existing files alone, create a new *.cpp file, and call a function from that *.cpp file within the main.c file before or during the main loop. Then do your C++ implementation within that created file.

     

    Code generated from CubeMX and FreeRTOS are designed for C and it's best to leave those alone unless you have significant experience on what is needed to convert them C++. There is no problem having C and C++ files in the same project, but you can't call or use C++ constructs from C files.