ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
int __init sam_init(void)
{
printk(KERN_INFO "Hello, world - this is the kernel speaking\n");
return 0;
}
module_init(sam_init);
file2.c
Quote:
#include <linux/kernel.h>
#include <linux/module.h>
#include "sam.h"
void __exit sam_exit(void)
{
printk(KERN_INFO "Short is the life of a kernel module\n");
}
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
The header file 'sam.h' is an empty file.
If I compile the above every thing goes fine. In case, I add a global variable in the header file like 'int global', I am getting the following error
Quote:
/home/greplinux/file2.o.bss+0x0): multiple definition of `global'
/home/greplinux/file1.o.bss+0x0): first defined here
make[2]: *** [/home/greplinux/file1file2.o] Error 1
Can you guys please explain the logic behind this error. I know, I am doing some thing wrong but not sure about it.
Header files are treated just like inline code in each source file. Including the same header in each source module will result in a variable named 'global' in each object module. This causes the linker to gag, as you have shown.
It is better to either not define variables in header files, or use conditionals in the header to prevent your circumstance. The compiler should create an instance of the variable in exactly one source module compilation. In all others, it should see merely a declaration.
To add a precision:
Declare the global variable you want to share in the header with the keyword "extern" (or in any peace of code that need to access it, since you can declare the same symbol many times). Then _define_ it in only one code file (one symbol can be defined only once).
As it goes for function, you're declaring them (just their signatures) before using it: tell the compiler that you're using a symbol, whatever it contents. Then you're defining the function, or you're using the function definition provided by a library, to tell the linker what the function does.
2 remarks:
Shared datas generally need to be protected against concurrent accesses.
Adding the "volatile" keyword to the variable declaration is a good idea, thus avoiding the compiler to optimize access to it, since optimization could break your algorithm (your assumption of "when" your application can access to the variable).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.