-2

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!

Phien
  • 148
  • 1
  • 14
  • Please show us what actually happens, rather than just saying that it does not work. Does it crash? Does it output nothing? Something, but the wrong thing? If so, what? – KRyan Aug 09 '12 at 04:56
  • 2
    I'm surprised you don't get an error when you build this, as you are defining the variable `bufJum` twice, _and_ spell `longjmp` wrong! 1: Don't put definitions in header files; 2: If you want to access a variable from multiple source files, don't make it `static`; 3: Avoid `setjmp`/`longjmp` as the plague! It doesn't play well with C++, and can in fact create lots of strange runtime errors if you're not **very** careful. – Some programmer dude Aug 09 '12 at 05:51
  • 1
    Also, saying "this code is incorrect" is not enough of a problem description. **What** happens when you compile/link/run it? If there are error messages please add them (verbatim, i.e. copy-paste) to the question. Besides, you are not using `setjmp`/`longjmp` is _multiple_ files, since you are _including_ one file into another the compiler will actually only see one file. – Some programmer dude Aug 09 '12 at 06:17
  • All of you not understand my question! The question is "HOW TO USE setjum and longjmp in multiple file". The setjum in one file and longjmp in other file. I explain my thought in code for more clearly. It is not "how run my code correctly" It is possible or not when use setjum and longjum in different files. And if i's possible, how to do that? Sorry for my english. – Phien Aug 09 '12 at 07:09
  • While it's in different files _for you_, the _compiler_ will see it as only _one_ file because of the pre-processor `#include` directive. So it really isn't multiple files! Also, when describing a problem don't just say it's not working, say _what_ is happening. – Some programmer dude Aug 09 '12 at 08:04
  • More precisely: the compiler works on a **Translation Unit**, which is the result of `#include`-ing multiple files into one. – MSalters Aug 09 '12 at 10:00

3 Answers3

1

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:

#include <setjmp.h>

extern jmp_buf bufJum;  // Note: `extern` to tell the compiler it's defined in another file

void func()
{
    longjmp(bufJum, 1);
}

main.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 << "1\n";
        func();
    }
    else
    {
        std::cout << "2\n";
    }

    return 0;
}

If you are using GCC to compile these files, you can e.g. use this command line:

$ g++ -Wall main.cpp myfunction.cpp -o myprogram

Now you have an executable program called myprogram which is made from two files.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

I don't know anything about setjmp, but you have at least one mistake in your code:

-#include <myfunction.h>
+#include "myfunction.h"
CyberDem0n
  • 14,545
  • 1
  • 34
  • 24
0

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.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • Sorry for my code, but i wan't to know possible or not possible when using setjmp and longjmp in different files. Don't mention on the variable. I just know how to use setjmp and longjmp in different files. And if it's possible, How to do that? Thank! – Phien Aug 09 '12 at 07:12