OSQA is unmaintained. Help us figure out where to go from here.

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

accept rate: 0%

edited 13 Apr '12, 10:19

multipleinterfaces's gravatar image


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.


answered 13 Apr '12, 11:33

Guy%20Harris's gravatar image

Guy Harris ♦♦
accept rate: 19%

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:

extern int my_global_variable;


#include "myproto-helper.h"


int my_global_variable;


#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).


answered 13 Apr '12, 10:16

multipleinterfaces's gravatar image

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



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



Asked: 13 Apr '12, 01:47

Seen: 2,269 times

Last updated: 15 Apr '12, 23:37

p​o​w​e​r​e​d by O​S​Q​A