C++ 中的信号处理
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
信号处理不是 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 中为
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