Results 1 to 6 of 6
My project is based on the program VLC media player, a multithreaded program. I wish to use a global variable as a counter in this program across various files and ...
- 01-07-2009 #1Just Joined!
- Join Date
- Nov 2008
- Posts
- 25
How to use global variable in a multithreaded program?
My project is based on the program VLC media player, a multithreaded program. I wish to use a global variable as a counter in this program across various files and folders. How should I do this?
1 of the websites i refer was
C Programming :: How to use a variable in multiple/different source files? Circuit Negma
but i cannot get it done in VLC media player. For example, in the VLC /modules/control/hotkeys.c, I don't know where the vlc_keys.h header file locates (which is 1 of the files i wish to include the global variable).
I had thought of writing another header file for the global variable, but i don't know how to include it in the makefile. So does anyone has idea on it?
Thanks a lot.
- 01-07-2009 #2
Source code directories are usually fairly well structured. You can always go to the top of the directory tree and run "find . -name vlc_keys.h" to find the file in that subtree.
Alternatively, you could always declare the variable in the file that controls it, and then have your other files declare it as extern. This will make the other files aware of the variable, but will only actually define it in one place.DISTRO=Arch
Registered Linux User #388732
- 01-08-2009 #3Just Joined!
- Join Date
- Nov 2008
- Posts
- 25
Do you means I do not need to declare the variable in a header file? I just need to declare it as extern in other files that need it once I have declared the variable in the file that controls it? How about if the .c file is in a directory different from the directory of the file that contains the variable declaration? How should I declare it in this .c file? Also declare it as an extern?
Thanks
- 01-10-2009 #4
So at the end of the day, GCC does not care what directory a file is in. Rather, you give it a set of files to compile, and it treats them as a big unit.
So imagine that I have two files. We'll call them "control" and "user":
control.c:
user.c:Code:int useroption= 0; void foo(void) { puts "Enter a number:"; scanf("%d", &useroption); }
Do you see how this works? user.c will use the variable from control.c, no matter what directories they are placed in.Code:extern int useroption; void bar(void) { switch(useroption) { 0: puts "You haven't entered an option yet."; break; default: printf("You selected option %d.\n", useroption); break; } }DISTRO=Arch
Registered Linux User #388732
- 01-11-2009 #5Just Joined!
- Join Date
- Nov 2008
- Posts
- 25
Thanks Cabhan. But I cannot compile the control.c file you posted.
Am i doing in a wrong way? The control.o file was not being created.Code:christyyim@christyyim-desktop:~/Desktop/c_source_code/GlobalVariableEg4$ gcc -c -g control.c christyyim@christyyim-desktop:~/Desktop/c_source_code/GlobalVariableEg4$ cd abcd/ christyyim@christyyim-desktop:~/Desktop/c_source_code/GlobalVariableEg4/abcd$ gcc -c -g user.c christyyim@christyyim-desktop:~/Desktop/c_source_code/GlobalVariableEg4/abcd$ cd .. christyyim@christyyim-desktop:~/Desktop/c_source_code/GlobalVariableEg4$ gcc -o control control.o abcd/user.o /usr/lib/gcc/i486-linux-gnu/4.1.3/../../../../lib/crt1.o: In function `_start': (.text+0x18): undefined reference to `main' collect2: ld returned 1 exit status

However, I had tried on others small programs i wrote and it worked. Now going to try in VLC source code, hopefully I won't get it wrong. God bless me...
- 01-11-2009 #6
Right: those files are not compileable because there is no main() function. I was just demonstrating a point. Feel free to add a main() that simply invokes foo() then bar().
DISTRO=Arch
Registered Linux User #388732


Reply With Quote
