C++ SD 总线源文件编译问题

C++ sd-bus source file compilation issue

本文关键字:编译 问题 源文件 总线 SD C++      更新时间:2023-10-16

我将bus-service.c available @ http://0pointer.net/blog/the-new-sd-bus-api-of-systemd.html 复制到C++源文件bus-service.cpp。如果我使用 g++ 编译.cpp文件,则会出现构建错误,有人可以帮助我克服构建问题吗?

用于构建的命令:

g++ bus-service.cpp -lsystemd

保存下面的代码 bus-service.c 文件不会给出以下命令的错误:

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: %sn", 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: %sn", 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: %sn", 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: %sn", 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: %sn", strerror(-r));
goto finish;
}
for (;;) {
/* Process requests */
r = sd_bus_process(bus, NULL);
if (r < 0) {
fprintf(stderr, "Failed to process bus: %sn", 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: %sn", 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 */
/*Below is the error log */
In file included from /usr/include/systemd/sd-bus.h:111:0,
from bus-service.cpp:4:
bus-service.cpp:43:9: error: expected primary-expression before u2018.u2019 token
SD_BUS_VTABLE_START(0),
^
bus-service.cpp:44:9: error: expected primary-expression before u2018.u2019 token
SD_BUS_METHOD("Multiply", "xx", "x", method_multiply, SD_BUS_VTABLE_UNPRIVILEGED),
^
bus-service.cpp:44:9: error: expected primary-expression before u2018.u2019 token
SD_BUS_METHOD("Multiply", "xx", "x", method_multiply, SD_BUS_VTABLE_UNPRIVILEGED),
^
bus-service.cpp:44:9: error: expected primary-expression before u2018.u2019 token
SD_BUS_METHOD("Multiply", "xx", "x", method_multiply, SD_BUS_VTABLE_UNPRIVILEGED),
^
bus-service.cpp:44:9: error: expected primary-expression before u2018.u2019 token
SD_BUS_METHOD("Multiply", "xx", "x", method_multiply, SD_BUS_VTABLE_UNPRIVILEGED),
^
bus-service.cpp:44:9: error: expected primary-expression before u2018.u2019 token
SD_BUS_METHOD("Multiply", "xx", "x", method_multiply, SD_BUS_VTABLE_UNPRIVILEGED),
^
bus-service.cpp:45:9: error: expected primary-expression before u2018.u2019 token
SD_BUS_METHOD("Divide",   "xx", "x", method_divide,   SD_BUS_VTABLE_UNPRIVILEGED),
^
bus-service.cpp:45:9: error: expected primary-expression before u2018.u2019 token
SD_BUS_METHOD("Divide",   "xx", "x", method_divide,   SD_BUS_VTABLE_UNPRIVILEGED),
^
bus-service.cpp:45:9: error: expected primary-expression before u2018.u2019 token
SD_BUS_METHOD("Divide",   "xx", "x", method_divide,   SD_BUS_VTABLE_UNPRIVILEGED),
^
bus-service.cpp:45:9: error: expected primary-expression before u2018.u2019 token
SD_BUS_METHOD("Divide",   "xx", "x", method_divide,   SD_BUS_VTABLE_UNPRIVILEGED),
^
bus-service.cpp:45:9: error: expected primary-expression before u2018.u2019 token
SD_BUS_METHOD("Divide",   "xx", "x", method_divide,   SD_BUS_VTABLE_UNPRIVILEGED),

将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                                      
}