[英]::write() and ::read() has not been declared error. Qt5 UNIX signal handling
所以,我正在尝试按照给定的教程
http://qt-project.org/doc/qt-5/unix-signals.html
捕获UNIX / Linux信号并在它们触发时执行Qt相关的操作。
它在qtDocs上,因此我认为这是合法的。
我现在的代码如下:
mydaemon.cpp
#include "mydaemon.h"
#include <QDebug>
#include <QObject>
#include <QSocketNotifier>
#include <csignal>
#include <sys/socket.h>
#include <iostream>
#include <stdio.h>
#include <signal.h>
//needed to not get an undefined reference to static members
int MyDaemon::sighupFd[2];
int MyDaemon::sigtermFd[2];
MyDaemon::MyDaemon(QObject *parent)
: QObject(parent)
{
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sighupFd))
qFatal("Couldn't create HUP socketpair");
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigtermFd))
qFatal("Couldn't create TERM socketpair");
snHup = new QSocketNotifier(sighupFd[1], QSocketNotifier::Read, this);
connect(snHup, SIGNAL(activated(int)), this, SLOT(handleSigHup()));
snTerm = new QSocketNotifier(sigtermFd[1], QSocketNotifier::Read, this);
connect(snTerm, SIGNAL(activated(int)), this, SLOT(handleSigTerm()));
}
MyDaemon::~MyDaemon() {}
void MyDaemon::hupSignalHandler(int)
{
qDebug() << "signal hup";
char a = '1';
::write(sighupFd[0], &a, sizeof(a));
}
void MyDaemon::termSignalHandler(int)
{
qDebug() << "signal term";
char a = '1';
::write(sigtermFd[0], &a, sizeof(a));
}
void MyDaemon::handleSigTerm()
{
snTerm->setEnabled(false);
char tmp;
::read(sigtermFd[1], &tmp, sizeof(tmp));
// do Qt stuff
qDebug() << "MyDaemon::handleSigTerm";
snTerm->setEnabled(true);
}
void MyDaemon::handleSigHup()
{
snHup->setEnabled(false);
char tmp;
::read(sighupFd[1], &tmp, sizeof(tmp));
// do Qt stuff
qDebug() << "MyDaemon::handleSigHup";
snHup->setEnabled(true);
}
现在,当我创建我的C ++类并尝试进行构建时,它会出现如下错误:
/ home / xxxx / Documents / Qt Projects / mainScreen / mydaemon.cpp:-1:在静态成员函数'static void MyDaemon :: termSignalHandler(int)'中:/ home / xxxx / Documents / Qt Projects / mainScreen / mydaemon.cpp :49:错误:尚未声明':: write':: write(sigtermFd [0],&a,sizeof(a)); ^
以此类推,对于:: read()或:: write()调用的所有实例。 我不确定自己在做什么错,我很感谢您的帮助:)
如果您使用的是“基本” Unix函数,则需要使用
#include <unistd.h>
这会给你read
, write
, open
, close
, sleep
, usleep
等诸多功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.