I have a global variable in a .c file in the dissectors/ folder. How do I use it in another .c file in the gtk/ folder? Every time I try using extern, I get an "undefined reference" error. I am able to use the same variable in another .c file in the epan/ folder. Is there anything I am missing?

asked 13 Apr '12, 01:47

vish's gravatar image

vish
0557
accept rate: 0%

edited 13 Apr '12, 10:19

multipleinterfaces's gravatar image

multipleinte...
1.2k91534


What is it you're trying to do?

If the global variable is a parameter setting for your dissector, try just registering it as a preference for the dissector; that way you don't need to write any GUI code for it - the existing Wireshark GUI code will let you set it from the Edit->Preferences dialog (and you can set it with the -o command-line flag in Wireshark or TShark).

If you're trying to write a tool to report statistics for your protocol, the tap mechanism could be used; you wouldn't need a global variable to pass data to a tap.

link

answered 13 Apr '12, 11:33

Guy%20Harris's gravatar image

Guy Harris ♦♦
11.6k227146
accept rate: 18%

I'm not sure what you aim to accomplish sharing something between a dissector and the epan and ui/gtk sections of Wireshark, but if I understand your question correctly, that is not your current problem.

The extern convention is only really one part of the puzzle you are looking at. At a basic level, the variable needs to both have shared visibility and a single storage area. Before going forward, note that this method will only work if your dissector is built in (which I think it is, since you say it's in epan/dissectors somewhere). Your dissector probably comes in two parts, packet-myproto.c and packet-myproto.h.
To declare the single storage area, you do as you probably already have by declaring int my_global_variable; somewhere in packet-myproto.c. Then, you need to make it accessible to other parts of the application (the shared visibility I mentioned above), and there are two options for doing this:
First, you could simply declare extern int my_global_variable; inside packet-myproto.h, and then #include "packet-myproto.h" in whatever file you need access to the variable, but this poses the issue of also including everything else included by your dissector (almost all of which is probably not needed by your gtk code).
A likely cleaner way of doing this would be to make another file (myproto-globalvariable.h or something) in the same directory with your dissector, then #include that file in packet-myproto.h and in the file for your gtk code. Then, inside the new file simply extern int my_globabl_variable; and you are ready to go. This provides you with the benefit of shared visibility without polluting your global namespace with dissection-related items where they are unneeded.

In brief, place the following in each of these files:
epan/dissectors/myproto-helper.h:

extern int my_global_variable;

epan/dissectors/packet-myproto.h:

#include "myproto-helper.h"

epan/dissectors/packet-myproto.c:

int my_global_variable;

path/to/your/gtk/stuff/my_gtk_stuff.c*:

#include "../../../../epan/dissectors/myproto-helper.h

*It could also be a .h file or what-have-you. Also remember to fix the include path (I'm almost certain ../../../../ is not correct for your case).

link

answered 13 Apr '12, 10:16

multipleinterfaces's gravatar image

multipleinte...
1.2k91534
accept rate: 12%

Thanks for the reply. But I did as u said but still getting undefined reference to `variable_name' Also its a hash table so I cant add it as a preference. Does it have something related to the Makefile, I mean do I need to give some path so that it looks for it while linking.

(15 Apr '12, 23:37) vish
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×338
×120
×13
×4

Asked: 13 Apr '12, 01:47

Seen: 1,320 times

Last updated: 15 Apr '12, 23:37

powered by OSQA