Hi,
My goal is to make a component that read from some sensors. Due to the requirements of the project, the code must be in C++ cause I need to use some pattern design, but the project(Main and other components, should be write with C).
file1.c-----Calls static function---->file2..cpp-----Create an A class----->A
when I try to compile, it simply doesn't work. The .cpp(file2) is not C++ code. And when I try to include into file2 class A, it says that it doesn't find the file(I Tripled check that its ok). #ifdef __cplusplus macro is not defined.
Inside the .mk files I have defined the:
COMPONENT_ADD_LDFLAGS=-lstdc++ -l$(COMPONENT_NAME)
It can not even include <string>, it says that it doesn't find the file.
What am I missing?
Thanks for the support!
Including C++ code into C project
-
- Posts: 95
- Joined: Tue Feb 21, 2017 10:17 pm
Re: Including C++ code into C project
Just because it's not C++ code, if you're putting it in a .cpp file, you're subjected to the C++ rules for everything, including name mangling.ervays wrote:Hi,
My goal is to make a component that read from some sensors. Due to the requirements of the project, the code must be in C++ cause I need to use some pattern design, but the project(Main and other components, should be write with C).
file1.c-----Calls static function---->file2..cpp-----Create an A class----->A
when I try to compile, it simply doesn't work. The .cpp(file2) is not C++ code. And when I try to include into file2 class A, it says that it doesn't find the file(I Tripled check that its ok). #ifdef __cplusplus macro is not defined.
Inside the .mk files I have defined the:
COMPONENT_ADD_LDFLAGS=-lstdc++ -l$(COMPONENT_NAME)
It can not even include <string>, it says that it doesn't find the file.
What am I missing?
Thanks for the support!
Unless you tell it otherwise, the C++ compiler (which is what you're using with a .cpp per normal makefile or SCons rules (presuming you're using PlatformIO there...) is presuming it is ALL C++ (C code within C++ is simply viewed as classless C++ code with all the rules that apply, including Name Mangling) and name mangling your code so that the linker can't find the references the C compiler makes within your block of code.
Bracket the .h file's for file2.cpp's definitions with an 'extern "C" { }' block for all C functions you're defining and make sure that that .h file is the first thing you include in file2.cpp. At the minimum, this should work right. Failing that, bracket the C functions and variables you're referencing in the .cpp file with the same in the .cpp file. This will turn name mangling off for those functions and variables.
Re: Including C++ code into C project
Thats not the problem I think. I've test the code in a simple C++ app and it works.
I think that there is smth about the environment cause __cplusplus is not defined.
SensorTask.c
SensorsController.h
SensorsController.cpp
SensorsController_Wrapper.h
SensorsController_Wrapper.cc
The problem is that there is no function called newSensorsController or deleteSensorsController. More precisely this is the error:
And that's should be smth about cplusplus or the includes(mk files or smth)
I think that there is smth about the environment cause __cplusplus is not defined.
SensorTask.c
Code: Select all
#include "../Task/SensorsTask.h"
#include "../Controller/SensorsController_Wrapper.h"
static xTaskHandle Sensors_Readings_task_hdl;
/**
* This function is gonna be the one that handle all the request from other task asking for sensor readings
*/
void sensorsReadingsTask(void *pvParameters)
{
while(1){
}
vTaskDelete(Sensors_Readings_task_hdl);
}
void sensors_Readings_Init() {
ESP_LOGI("Sensors", "Initializing Sensor_Readings Task");
struct SensorsController* c = newSensorsController();
deleteSensorsController(c);
/* Start BT configuration task*/
xTaskCreate(sensorsReadingsTask, "SensorReadings Task", 8192, NULL, 5,&Sensors_Readings_task_hdl);
}
SensorsController.h
Code: Select all
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
class SensorsController {
public:
SensorsController();
virtual ~SensorsController();
};
Code: Select all
SensorsController::SensorsController() {
ESP_LOGI("GasSensorsController", "*******************SIIIIIIIIIIIIIIIIIIIIII***************");
}
SensorsController::~SensorsController() {
// TODO Auto-generated destructor stub
}
Code: Select all
#ifdef __cplusplus
extern "C" {
#endif
typedef struct SensorsController SensorsController;
SensorsController* newSensorsController();
void deleteSensorsController(SensorsController* v);
#ifdef __cplusplus
}
#endif
Code: Select all
#include "SensorsController.h"
#include "SensorsController_Wrapper.h"
extern "C" {
SensorsController* newSensorsController() {
return new SensorsController();
}
void deleteSensorsController(SensorsController* v) {
delete v;
}
}
The problem is that there is no function called newSensorsController or deleteSensorsController. More precisely this is the error:
Code: Select all
$ make -j8
CC src/Task/SensorsTask.o
AR libSensorsReadings.a
LD app-template.elf
.../build/SensorsReadings\libSensorsReadings.a(SensorsTask.o):(.literal.sensors_Readings_Init+0x18): undefined reference to `newSensorsController'
...SensorsReadings\libSensorsReadings.a(SensorsTask.o):(.literal.sensors_Readings_Init+0x1c): undefined reference to `deleteSensorsController'
.../SensorsReadings\libSensorsReadings.a(SensorsTask.o): In function `sensors_Readings_Init':
.../SensorsReadings/src/Task/SensorsTask.c:41: undefined reference to `newSensorsController'
.../components/SensorsReadings/src/Task/SensorsTask.c:42: undefined reference to `deleteSensorsController'
collect2.exe: error: ld returned 1 exit status
make: *** [.../project.mk:323: ..../build/app-template.elf] Error 1
And that's should be smth about cplusplus or the includes(mk files or smth)
Re: Including C++ code into C project
I think the SensorsController_Wrapper.cc file doesn't get compiled at all. ESP-IDF build system assumes .c extension for C source files and .cpp for C++ source files. You can explicitly list the source files for the given component if you'd like, please see the build system documentation for that.
Re: Including C++ code into C project
Yes, it was the .cc extension.
Thanks for evrthng. This was really frustrating. I'm sort of newby chip programmer, I'm used to plain C++ development.
Cheers!
Thanks for evrthng. This was really frustrating. I'm sort of newby chip programmer, I'm used to plain C++ development.
Cheers!
Who is online
Users browsing this forum: No registered users and 141 guests