LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-29-2010, 08:05 AM   #1
greplinux
Member
 
Registered: Jun 2007
Posts: 118

Rep: Reputation: 17
Building Linux modules.


I am facing a problem while building a kernel module.

file1.c:
Quote:
#include <linux/kernel.h>
#include <linux/module.h>
#include "sam.h"

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");
}

module_exit(sam_exit);
Makefile
Quote:
obj-m += file1file2.o
file1file2-objs := file1.o file2.o

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.

Thanks,
 
Old 09-29-2010, 10:39 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

--- rod.
 
Old 09-29-2010, 11:50 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 09-29-2010, 03:45 PM   #4
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
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).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Skipping modules compilation while building linux kernel ramukaka Linux - Kernel 1 09-03-2010 11:15 AM
problems building modules chewbo Linux - Hardware 0 01-17-2008 12:09 PM
Questions about building a new kernel - using /usr/src/linux and rc.modules Nylex Slackware 5 10-11-2006 11:09 AM
Building modules into a kernel DJNolz83 Linux - Kernel 4 09-13-2006 05:11 PM
Building modules for the kernel... Nichole_knc Slackware 3 04-13-2004 08:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration