C++ SD 总线源文件编译问题
C++ sd-bus source file compilation issue
我将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
}
相关文章:
- Rextester 语言编译C++问题
- C++ 多级虚拟继承编译问题
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用 CImg 库的 std::min 和 std::max 的编译问题
- Qt Visual Studio 2015 加载项编译问题
- libssh 问题编译问题未定义的错误
- 在 armhf 上使用 cmake 在 qt 上的编译问题 "Syntax error: wird unexpected (expecting " ) " "
- LLVM 万花筒教程 JIT 编译问题
- PX 转换例程编译问题
- 模板类编译问题与 typedef 变量
- 在 Ubuntu 16.04 LTS 上使用 c++ 编译问题?
- 编译问题与 Ubuntu Linux
- C++编译问题错误 - C2332:<类:缺少标记名称>
- AIX C++映射编译问题
- Ubuntu 下的 Cmake 和 Intel IPP 的编译问题
- 如何解决我在 Ubuntu 19.04 下使用 GLFW 时遇到的这个编译问题?
- wxWebView在Ubuntu上的编译问题
- C++constexpr编译问题
- CMake - OpenVR 编译问题
- C++ SD 总线源文件编译问题