2020-02-19 08:07 AM
I am writing C++ projects using STMCubeIDE and running into a problem when using classes or other C++ features.
Even when choosing that the project be a C++ project, I can't build the code. Manually changing the file extensions to .cpp makes the code compile, but then breaks the link to generating code with STMCube. If I want to make changes to my pinout configuration I have to manually change the file names back to .c and then build the code, then manually change back to .cpp.
Is there a better way to do this?
2020-02-19 11:33 AM
Fact is automated code generation / update is requiring to rely on a constant file set. If renaming main.c to main.cpp automation is lost then.
Such sounds like a bad news but no issue in fact hopefully some way of working with exist (at least according my own experience) !
1) Hopefully code generation result is C++ compliant
2) IDE support C or C++ projects. Having set your target project's language (either at project creation or Thanks project contexttual "Convert to C++ / C") proper compiler is invoked based on file extensions ( .c or .cpp). And hopefully file extensions mix within a project is supported.
3) Tool suite is fine then up to you to take some care writing your code. Such may sounds like a pain but good in fact to get some structured code finally ...
Here after is my way to mix C & C++ keeping benefit of code generation / update automation. By the way no magic here just rely on your favorite search engine to get some tricks mixing C & C++ code.
Process.h file =
#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif
EXTERNC void doMyCppProcess(void);
#undef EXTERNC
Process.cpp file =
#include "Process.h"
extern "C" {
#include "MyCFunctionsHeader.h"
}
void doMyCppProcess(void) {
/*
* My code here
*/
}
Main.c file =
/* Private includes ----------------------------------------------------------*/
#include "Process.h"
/* USER CODE END Includes */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
doMyCppProcess();
/* USER CODE END 1 */
2020-02-19 12:30 PM
A similar discussion was here: https://community.st.com/s/question/0D50X0000BcREG3SQO/how-do-i-compile-c-on-stm32cubeide
I leave the generated cod as is and call a C++ entry function (or two setup() and loop() ) right before the main loop in user code sections. Luckily the generated headers can be included from C++ code so I can use the APIs from C++.
hth
KnarfB