C++ 中的信号处理

发布于 2024-11-17 11:10:19 字数 1119 浏览 3 评论 0原文

void (*)(int) 类型的参数与 __sighnd64_t 类型的参数不兼容

下面是我的简单代码:

#include <iostream>
#include <string>
#include <signal.h>
#include <ctype>
#include <stdlib.h>
#include <stdio.h>
typedef struct mystrcut
{

  int a;
  char *b;

} mystr;

void set_string ( char **, const char * );
void my_handler(int s)
{
    printf("Caught signal %d\n",s);
    exit(1);

}

int main()
{
    const std::string str1[] = {"hello1", "hello2"};
    char str2[50];
    size_t size1 = str1[1].size();
    cout << size1;
    memcpy (str2, str1[1].c_str(), size1);

    cout << str2;
    mystr *m = NULL;
    m = new mystrcut;
    m->a = 5;

    set_string(&m->b, "hello");

    cout << m->b;
    delete []m->b;
//    void (*prev_fn)(int);
    signal (SIGINT,my_handler);
    return 0;
}
void set_string ( char **a, const char *b)
{
    *a = new char [strlen(b)+1];
    strcpy (*a, b);
}

我正在 openvms 上工作。我可以通过某种类型转换来避免编译错误吗?我的编译器需要 `__sighnd64_t __64_signal(int, __sighnd64_t);

添加处理程序 extern c 已经起作用。谢谢

Argument of type void (*)(int) is incompatible with parameter of type __sighnd64_t

Below is my simple code:

#include <iostream>
#include <string>
#include <signal.h>
#include <ctype>
#include <stdlib.h>
#include <stdio.h>
typedef struct mystrcut
{

  int a;
  char *b;

} mystr;

void set_string ( char **, const char * );
void my_handler(int s)
{
    printf("Caught signal %d\n",s);
    exit(1);

}

int main()
{
    const std::string str1[] = {"hello1", "hello2"};
    char str2[50];
    size_t size1 = str1[1].size();
    cout << size1;
    memcpy (str2, str1[1].c_str(), size1);

    cout << str2;
    mystr *m = NULL;
    m = new mystrcut;
    m->a = 5;

    set_string(&m->b, "hello");

    cout << m->b;
    delete []m->b;
//    void (*prev_fn)(int);
    signal (SIGINT,my_handler);
    return 0;
}
void set_string ( char **a, const char *b)
{
    *a = new char [strlen(b)+1];
    strcpy (*a, b);
}

I am working on openvms. Can I avoid the compilation error by some kind of type casting? My compiler expects `__sighnd64_t __64_signal(int, __sighnd64_t);

Adding around the handler extern c has worked. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2024-11-24 11:10:19

信号处理不是 C++,而是 C。这个错误有点奇怪...

在这种情况下,尝试在处理程序周围使用 extern "C"(将其定义为 C 函数),如 https://stackoverflow.com/users/775806/nm在评论中说。来自 openvms 的现代 内部已包含 extern "C":http://wasd.vsm.com.au/conan/sys$common/syslib/decc$rtldef.tlb?key=SIGNAL&title=Library%20/sys$common/syslib/decc$rtldef.tlb& ;referer=http%3A/wasd.vsm.com.au/conan/sys$common/syshlp/helplib.hlb

HP 文档仅说明有关 C,而不是 C++。

另一个文档说必须声明信号处理程序(捕获器)在 C 中为

 void func(int signo);

Signal handling is not a C++, but a C. This error is bit strange...

In such cases, try to use extern "C" around your handler (to define it as C function), as https://stackoverflow.com/users/775806/n-m said in comments. Modern <signal.h> from openvms already has extern "C" inside: http://wasd.vsm.com.au/conan/sys$common/syslib/decc$rtldef.tlb?key=SIGNAL&title=Library%20/sys$common/syslib/decc$rtldef.tlb&referer=http%3A/wasd.vsm.com.au/conan/sys$common/syshlp/helplib.hlb.

HP docs says only about C, not a C++.

Another doc says that signal handler (catcher) must be declared in C as

 void func(int signo);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文