在多个文件中使用jmp_buf
Use jmp_buf in multiple file
为了清楚起见,请查看我的样本
我有两个文件:main.cpp和myfunction.h
这是主要的.cpp
#include <setjmp.h>
#include <myfunction.h>
int main()
{
if ( ! setjmp(bufJum) ) {
printf("1");
func();
} else {
printf("2");
}
return 0;
}
这是myfunction.h
#include <setjmp.h>
static jmp_buf bufJum;
int func(){
longjum(bufJum, 1);
}
现在,我想让我的屏幕打印"1",然后打印"2",但此代码不正确!求求你,帮帮我!非常感谢!
如果要将其放在多个文件中,则需要创建两个源文件,而不是单个源文件和一个头文件
myfunction.cpp:
#include <setjmp.h>
extern jmp_buf bufJum; // Note: `extern` to tell the compiler it's defined in another file
void func()
{
longjmp(bufJum, 1);
}
主.cpp:
#include <iostream>
#include <setjmp.h>
jmp_buf bufJum; // Note: no `static`
void func(); // Function prototype, so the compiler know about it
int main()
{
if (setjmp(bufJum) == 0)
{
std::cout << "1n";
func();
}
else
{
std::cout << "2n";
}
return 0;
}
如果您使用 GCC 编译这些文件,则可以使用以下命令行:
$ g++ -Wall main.cpp myfunction.cpp -o myprogram
现在你有一个名为myprogram
的可执行程序,它由两个文件组成。
我对 setjmp 一无所知,但您的代码中至少有一个错误:
-#include <myfunction.h>
+#include "myfunction.h"
您在 .h 文件中定义了非内联函数。 虽然不违法,但这几乎总是错误的。
您在 .h 文件中定义了静态全局变量。 虽然不违法,但这几乎总是错误的。
相关文章:
- x86 - 为什么编译器在下一条指令中插入看似毫无意义的JMP?
- 如何强制 GCC 使用 jmp 指令而不是 ret?
- 如何从Google Proto Buf消息中的属性名称中找到消息类型
- 将Windows SYSTEMTIME转换为字符串或字符buf,C++与用户的"Region and Language"格式?
- 结果是不同的;C++ 中的 recv(sock,buf,sizeof(buf),0) 和 strlen(buf)
- C 如何在写入中向BUF添加消息
- ATmega8不支持JMP指令
- *buf++ = *buf + 10 - Explanation
- 我想从文件中读取数据块并将其存储在矢量<string> buf 中。我做错了什么?
- 如何为文本和箱缓冲区设置 WSABUF.buf
- OpenCV 错误:断言失败 (buf.data && buf.isContinu()) in cv::imdecode_, file ..\..\..\..\openCV
- JMP间接性的意义何在
- Detours - jmp 00000000
- GCC 内联程序集 JMP 地址;裸函数
- the use of XMLString::release( XMLCh ** buf)
- 如何将Boost::asio::buffer(buf,size)与Boost绑定一起使用
- 将unsigned char *buf=NULL转换为Pascal
- (cin >> buf && !buf.empty()) 中的第二个条件是多余的吗?
- Istream::tellg()与我的自定义流buf类一起使用时返回-1
- Base*p=new(buf)Base;的含义是什么;