For clearly, please view my sample
I have two files: main.cpp and myfunction.h
This is main.cpp
#include <setjmp.h>
#include <myfunction.h>
int main()
{
if ( ! setjmp(bufJum) ) {
printf("1");
func();
} else {
printf("2");
}
return 0;
}
This is myfunction.h
#include <setjmp.h>
static jmp_buf bufJum;
int func(){
longjum(bufJum, 1);
}
Now, I want my screen print "1" and then print "2", but this code is uncorrect! Please, help me! Thank you so much!
You have a non-inline function defined in a .h file. While not illegal, this is pretty much always wrong.
You have a static global variable defined in a .h file. While not illegal, this is pretty much always wrong.
If you want to have it in multiple files, then you need to create two source files, not a single source file and a header file
myfunction.cpp:
main.cpp:
If you are using GCC to compile these files, you can e.g. use this command line:
Now you have an executable program called
myprogram
which is made from two files.I don't know anything about setjmp, but you have at least one mistake in your code: