Problems detecting C++ header file

Hey guys,

I just wanted to try out Platformio with my already existing STM32 code, but building always fails.

Here is the problematic Header-File:

accumulator.hpp

class test{
private:
    uint16_t num;
public:
    float test();
};

This is the Error-Mesasge:

In file included from include/main.h:61:0,
from include/crc16.h:9,
from src\MSI.c:11:
include/accumulator.hpp:1:1: error: unknown type name ‘class’
class test{

It seems as if it has problems to recognize that .hpp is C++ code as the error says “class” is an unknown type.

Do you have any idea how to fix this?
All I could find was people having problems with .cpp files.

Thanks!

Is it possible that you have a syntax error in one of the files in the error stack there, which is causing the class keyword to be recognised incorrectly?

I’ve had that sort of thing myself a couple of times.

I realise you’ve compiled the code in the past without problem. Maybe the toolchain is different and finding something not found originally.

Just a thought.

Cheers,
Norm.

1 Like

Thanks for your response!

I wasn’t able to compile it in CubeIDE for other reasons, but this header was never a problem.

All the other errors are also connected to the class not beeing recognized.

EDIT:

To ensure I don’t have a syntax error I stipped down my program to just two files:

accumulator.hpp

class accumulator{
private:
    int num1;
public:
    float test1();
};

.

main.c

#include "accumulator.hpp"
int main(){
    return 0;
}

But I still get this error:

Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/genericSTM32F405RG.html
PLATFORM: ST STM32 8.0.0 > STM32F405RG (128k RAM. 1024k Flash)  
HARDWARE: STM32F405RG 168MHz, 128KB RAM, 1MB Flash
DEBUG: Current (blackmagic) External (blackmagic, jlink, stlink)
PACKAGES:
 - framework-stm32cube 2.0.181130
 - tool-ldscripts-ststm32 0.1.0
 - toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 27 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\genericSTM32F405RG\src\main.o
In file included from src\main.c:1:0:
include/accumulator.hpp:1:1: error: unknown type name 'class'
 class accumulator{
 ^~~~~
include/accumulator.hpp:1:18: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
 class accumulator{
                  ^
Compiling .pio\build\genericSTM32F405RG\FrameworkHALDriver\Src\stm32f4xx_hal_dcmi_ex.o
Compiling .pio\build\genericSTM32F405RG\FrameworkHALDriver\Src\stm32f4xx_hal_dfsdm.o
Compiling .pio\build\genericSTM32F405RG\FrameworkHALDriver\Src\stm32f4xx_hal_dma.o
*** [.pio\build\genericSTM32F405RG\src\main.o] Error 1
=============================================================================================== [FAILED] Took 4.06 seconds =====

You are including a C++ header containg classes in a C file.

Since #include is just a fancy way of copy-pasting the file content into the includer, you now have a C file in which you define a C++ class. That can impossibly work.

3 Likes

Thank’s a lot for pointing this out!! :star_struck:

It’s crazy how such small things can cost you hours of your lifetime :sweat_smile:

1 Like