Using C API under C++: error: taking address of rvalue #918
-
I am currently trying to use the C-API of flecs in an C++17 project to have full control over it (i dont want to use the C++ API). I initiate my world with the following code: ecs_world_t *world = ecs_init();
ecs_singleton_set(world, EcsUnit, {0});
ecs_singleton_set(world, EcsRest, {0});
ecs_singleton_set(world, EcsMonitor, {0}); But my compiler (GCC 12) gives multiple errors in this line, maybe the usage of {0} is only in C supported and not in C++ but i can't find any alternative...
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The reason this happens is because If you'd really like to use the C API from C++, consider using the "naked" C functions in the include/flecs.h file, like It's a bit more typing, but you'll be using plain C functions: // Initialize component to 0
EcsRest r = {};
// ecs_id(EcsRest) returns the name of the global variable that holds the component id for EcsRest
// A singleton is a component that's assigned to itself, which is why entity and id are both ecs_id(EcsRest)
ecs_set_id(world, ecs_id(EcsRest), ecs_id(EcsRest), sizeof(EcsRest), &r); |
Beta Was this translation helpful? Give feedback.
-
That did solve it, i now decided to switch to the C++ API to remove some extern C markings and to better integrate it. Thanks for the help! |
Beta Was this translation helpful? Give feedback.
The reason this happens is because
ecs_set
is part of a set of macro helpers that provide a sort of type safety for the C API, but they rely on features that are not available in C++ (such as taking the address of a temporary).If you'd really like to use the C API from C++, consider using the "naked" C functions in the include/flecs.h file, like
ecs_set_id
:https://github.com/SanderMertens/flecs/blob/master/include/flecs.h#L2529
It's a bit more typing, but you'll be using plain C functions: