How do I convert my C++ function pointer to a C function pointer?
-
I am writing code for an ARM processor and compiling with GCC inside Eclipse IDE. When the application wants something to get done, it needs to create a task and queue it:
typedef uint32_t (*task_t)(uint8_t* data, uint16_t dataSizeBytes);
void queueTask(task_t task, uint8_t* data, uint16_t dataSizeBytes);Now I want to be able to queue C++ tasks inside the same task queue:
#include
using namespace std;
using taskCpp_t = function ;
void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes);The compiler complains when I simply try to cast my C++ function pointer into a C function pointer:
void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes) {
queueTask((task_t)task, NULL, 0); // Compiler complains!!!
}Does anybody know what I can do to be able to queue taskCpp_t tasks into the same queue as the task_t are queued into?
-
I am writing code for an ARM processor and compiling with GCC inside Eclipse IDE. When the application wants something to get done, it needs to create a task and queue it:
typedef uint32_t (*task_t)(uint8_t* data, uint16_t dataSizeBytes);
void queueTask(task_t task, uint8_t* data, uint16_t dataSizeBytes);Now I want to be able to queue C++ tasks inside the same task queue:
#include
using namespace std;
using taskCpp_t = function ;
void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes);The compiler complains when I simply try to cast my C++ function pointer into a C function pointer:
void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes) {
queueTask((task_t)task, NULL, 0); // Compiler complains!!!
}Does anybody know what I can do to be able to queue taskCpp_t tasks into the same queue as the task_t are queued into?
-
I am writing code for an ARM processor and compiling with GCC inside Eclipse IDE. When the application wants something to get done, it needs to create a task and queue it:
typedef uint32_t (*task_t)(uint8_t* data, uint16_t dataSizeBytes);
void queueTask(task_t task, uint8_t* data, uint16_t dataSizeBytes);Now I want to be able to queue C++ tasks inside the same task queue:
#include
using namespace std;
using taskCpp_t = function ;
void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes);The compiler complains when I simply try to cast my C++ function pointer into a C function pointer:
void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes) {
queueTask((task_t)task, NULL, 0); // Compiler complains!!!
}Does anybody know what I can do to be able to queue taskCpp_t tasks into the same queue as the task_t are queued into?
You need to let C++ know that the function call is using C naming conventions, and not C++ name mangling. e.g.
task.h
#ifdef __cplusplus
extern "C" {
#endiftypedef uint32_t(*task_t)(uint8_t* data, uint16_t datasizeBytes);
void queueTask(task_t task, uint8_t* data, uint16_t datasizeBytes);#ifdef __cplusplus
}
#endifKeep Calm and Carry On
-
I am writing code for an ARM processor and compiling with GCC inside Eclipse IDE. When the application wants something to get done, it needs to create a task and queue it:
typedef uint32_t (*task_t)(uint8_t* data, uint16_t dataSizeBytes);
void queueTask(task_t task, uint8_t* data, uint16_t dataSizeBytes);Now I want to be able to queue C++ tasks inside the same task queue:
#include
using namespace std;
using taskCpp_t = function ;
void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes);The compiler complains when I simply try to cast my C++ function pointer into a C function pointer:
void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes) {
queueTask((task_t)task, NULL, 0); // Compiler complains!!!
}Does anybody know what I can do to be able to queue taskCpp_t tasks into the same queue as the task_t are queued into?
-
I am writing code for an ARM processor and compiling with GCC inside Eclipse IDE. When the application wants something to get done, it needs to create a task and queue it:
typedef uint32_t (*task_t)(uint8_t* data, uint16_t dataSizeBytes);
void queueTask(task_t task, uint8_t* data, uint16_t dataSizeBytes);Now I want to be able to queue C++ tasks inside the same task queue:
#include
using namespace std;
using taskCpp_t = function ;
void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes);The compiler complains when I simply try to cast my C++ function pointer into a C function pointer:
void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes) {
queueTask((task_t)task, NULL, 0); // Compiler complains!!!
}Does anybody know what I can do to be able to queue taskCpp_t tasks into the same queue as the task_t are queued into?
To show how different the two things are try this:
int main ()
{
taskCpp_t t;
cout << "taskCpp_t size: " << sizeof (taskCpp_t) << '\n';
cout << "task_t size: " << sizeof (task_t) << '\n';
}I get:
taskCpp_t size: 64
task_t size: 8but it is up the compiler what a function object should contain.
Mircea
-
You may use a
C++ static
function as task. That would be equivalent to a standardC
function.I'm hoping to be able to use the function like this:
queueTaskCpp([](uint8_t* data, uint16_t dataSizeBytes) {
// My function implementation in PURE C (no C++) goes here...
}, NULL, 0);I would assume the above function implementation would become static, right? God forbid if the function gets implemented on the stack, obviously that would mean I can't use this concept at all since the function implementation on the stack will most likely be corrupt when it's time to execute the queued function. I'm not planning to use any classes or object orientedness, I'm only using C++ to be able to pass anonymous functions like this.