cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to port Arduino library (C++ library ) to STM32 CubeIDE (C project)

SDel.19
Associate II

Hi all!

I used to "work" with Arduino environment in the past. Now I passed to Eclipse based IDE and in this specific case I'm using STM32CUBEIDE to develop a C project for an F100 series MCU.

In the Arduino world I used often a library written in C++ (header file and cpp file) and I would really like to use the same library in this case too, obviously with the due modification because of the different hardware but it's not this the point.

This is the library: https://github.com/mathertel/OneButton

In order to do that I added 2 new files in my project called say mylib.h and mylib.cpp. Than I copied the original library code in theese files. I also included the header file in main.c and build the project but I get the following errors:

I believe the problem is in the C++ language and I have to say I've never used C++ in STM32CUBEIDE so maybe I miss some steps...

p.s. the errors are in the images

4 REPLIES 4
TDK
Guru

> I also included the header file in main.c

The library is written in C++. You cannot include a C++ header within a C file. The C compiler doesn't know about classes.

You'll need to rename main.c to main.cpp or find another method.

If you feel a post has answered your question, please click "Accept as Solution".
Nikita91
Lead II

The trick is to jump from C in main.c to a C++ code.

Define a C++ file like cppentry.ccp wich contain something like:

extern "C" void cppEntry (uintptr_t arg)
{
	// Delete these lines and insert your C++ code here
	(void) arg ;
	cppTest () ;
}

In the main .c insert:

//	The C++ entry, for demo purpose.
//	You can start your C++ code from there
void cppEntry (uintptr_t arg) ;
 
 
int main ()
{
	// To start the C++ application: call cppEntry()
	cppEntry (arg) ;
 
	return 0 ;
}

SDel.19
Associate II

I will try, thank you guys!

By doing so it is throwing an another error!