我将bus-service.c可用@ http://0pointer.net/blog/the-new-sd-bus-api-of-systemd.html复制到C ++源文件bus-service.cpp。如果我使用g ++编译.cpp文件编译获取构建错误有人可以帮助我克服构建问题吗?
用于构建的命令:
android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
...
defaultConfig {
...
minSdkVersion 19
...
保存以下代码bus-service.c文件不会出现以下命令错误:
g++ bus-service.cpp -lsystemd
以下是上述链接的源代码:
g++ bus-service.cpp -lsystemd
/* start of bus-service.cpp */ #include < stdio.h > #include < stdlib.h > #include < errno.h > #include < systemd / sd - bus.h > static int method_multiply(sd_bus_message * m, void * userdata, sd_bus_error * ret_error) { int64_t x, y; int r; /* Read the parameters */ r = sd_bus_message_read(m, "xx", & x, & y); if (r < 0) { fprintf(stderr, "Failed to parse parameters: %s\n", strerror(-r)); return r; } /* Reply with the response */ return sd_bus_reply_method_return(m, "x", x * y); } static int method_divide(sd_bus_message * m, void * userdata, sd_bus_error * ret_error) { int64_t x, y; int r; /* Read the parameters */ r = sd_bus_message_read(m, "xx", & x, & y); if (r < 0) { fprintf(stderr, "Failed to parse parameters: %s\n", strerror(-r)); return r; } /* Return an error on division by zero */ if (y == 0) { sd_bus_error_set_const(ret_error, "net.poettering.DivisionByZero", "Sorry, can't allow division by zero."); return -EINVAL; } return sd_bus_reply_method_return(m, "x", x / y); } /* The vtable of our little object, implements the net.poettering.Calculator interface */ static const sd_bus_vtable calculator_vtable[] = { SD_BUS_VTABLE_START(0), SD_BUS_METHOD("Multiply", "xx", "x", method_multiply, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("Divide", "xx", "x", method_divide, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_VTABLE_END }; int main(int argc, char * argv[]) { sd_bus_slot * slot = NULL; sd_bus * bus = NULL; int r; /* Connect to the user bus this time */ r = sd_bus_open_user( & bus); if (r < 0) { fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r)); goto finish; } /* Install the object */ r = sd_bus_add_object_vtable(bus, & slot, "/net/poettering/Calculator", /* object path */ "net.poettering.Calculator", /* interface name */ calculator_vtable, NULL); if (r < 0) { fprintf(stderr, "Failed to issue method call: %s\n", strerror(-r)); goto finish; } /* Take a well-known service name so that clients can find us */ r = sd_bus_request_name(bus, "net.poettering.Calculator", 0); if (r < 0) { fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r)); goto finish; } for (;;) { /* Process requests */ r = sd_bus_process(bus, NULL); if (r < 0) { fprintf(stderr, "Failed to process bus: %s\n", strerror(-r)); goto finish; } if (r > 0) /* we processed a request, try to process another one, right-away */ continue; /* Wait for the next request to process */ r = sd_bus_wait(bus, (uint64_t) - 1); if (r < 0) { fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r)); goto finish; } } finish: sd_bus_slot_unref(slot); sd_bus_unref(bus); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; } /* end of bus-service.c */
答案 0 :(得分:0)
用下面指示的宏替换SD_BUS_VTABLE_START,SD_BUS_METHOD和SD_BUS_VTABLE_END,我能够解决构建问题。
#define __SD_BUS_VTABLE_START(_flags) { _SD_BUS_VTABLE_START, _flags, .x = {.start = {sizeof(sd_bus_vtable)}}}
#define __SD_BUS_METHOD_WITH_OFFSET(_member, _signature, _result, _handler, _offset, _flags) \
{ \
_SD_BUS_VTABLE_METHOD, \
_flags, \
.x = {.method = {_member, \
_signature, \
_result, \
_handler, \
_offset}} \
}
#define __SD_BUS_METHOD(_member, _signature, _result, _handler, _flags) \
__SD_BUS_METHOD_WITH_OFFSET(_member, _signature, _result, _handler, 0, _flags)
#define __SD_BUS_SIGNAL(_member, _signature, _flags) \
{ \
_SD_BUS_VTABLE_SIGNAL, \
_flags, \
.x = {.signal = {_member, \
_signature}} \
}
#define __SD_BUS_PROPERTY(_member, _signature, _get, _offset, _flags) \
{ \
_SD_BUS_VTABLE_PROPERTY, \
_flags, \
.x = {.property = {(const char*)_member, \
(const char*)_signature, \
(sd_bus_property_get_t)_get, \
(sd_bus_property_get_t)0, \
(size_t)_offset}} \
}
#define __SD_BUS_WRITABLE_PROPERTY(_member, _signature, _get, _set, _offset, _flags) \
{ \
_SD_BUS_VTABLE_WRITABLE_PROPERTY, \
_flags, \
.x = {.property = {_member, \
_signature, \
_get, \
_set, \
_offset}} \
}
#define __SD_BUS_VTABLE_END \
{ \
_SD_BUS_VTABLE_END \
}