LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 04-22-2018, 02:31 PM   #1
alkhimey
LQ Newbie
 
Registered: Nov 2006
Posts: 7

Rep: Reputation: 0
Question How to compile a single source file with the correct flags?


I am developing kernel module and want to perform the following:
1. Compile a source file into an object file with all correct flags. The source file includes kernel headers.
2. Produce a loadable kernel module by linking the object file with a 'main' of my kernel module.

To simplify, I want a statically linked library for loadable modules.

I guess that for step 1 I need to use kbuild makefile. The problem is that I was not able to figure out how to do it.


Here is the most obvious attempt:

Code:
obj-m: myfile.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$PWD/ myfile.o

Last edited by alkhimey; 04-22-2018 at 02:32 PM.
 
Old 04-23-2018, 07:37 AM   #2
Delcaran
Member
 
Registered: Dec 2013
Location: ud.fvg.it
Distribution: Slackware64 14.2
Posts: 44

Rep: Reputation: 27
Code:
make
is a tool for executing actions in a specific order. In your case this is overkill.

If you want to compile a single source, you should use the compiler. So, assuming your source is
Code:
myfile.c
and kernel sources are in
Code:
/src/$(shell uname -r)/
if you launch this command

gcc -I/src/$(shell uname -r) myfile.c

you should obtain
Code:
myfile.o
in current dir.

This should answer point 1.

I don't know anything about kernel programming, or creating modules, etc. so for point 2 you have to wait someone else.
 
Old 04-23-2018, 01:34 PM   #3
alkhimey
LQ Newbie
 
Registered: Nov 2006
Posts: 7

Original Poster
Rep: Reputation: 0
Delcaran, unfortunately this will not quite cut it.

Code:
$ gcc -I/usr/src/linux-headers-4.13.0-39-generic/include myfile.c 
In file included from /usr/src/linux-headers-4.13.0-39-generic/include/linux/printk.h:7:0,
                 from myfile.c:42:
/usr/src/linux-headers-4.13.0-39-generic/include/linux/linkage.h:7:25: fatal error: asm/linkage.h: No such file or directory
 #include <asm/linkage.h>
                         ^
compilation terminated.
One needs to pass gcc a set of correct flags and preprocessor macro flags to properly compile for the kernel.
When building the whole tree or a loadable kernel module then kbuild system handles all these.
 
Old 04-24-2018, 05:11 AM   #4
Delcaran
Member
 
Registered: Dec 2013
Location: ud.fvg.it
Distribution: Slackware64 14.2
Posts: 44

Rep: Reputation: 27
gcc is only complaining about a missing inclusion file, not about macros or other things.
If you can find the file linkage.h in the tree of kernel sources, you can pass the correct value to the -I parameter of gcc.
Then maybe other problems will emerge, but you can tackle them one by one.

EDIT:
looking at the tree in https://git.kernel.org/pub/scm/linux...h=linux-4.13.y I see that directory asm does not exists, but exists asm-generic... Maybe yes, kbuild is doing some voodoo, but you can try to create a symlink like
Quote:
cd /usr/src/linux-headers-4.13.0-39-generic/include/ ; ln -s asm-generic asm
EDIT2:
Looking at Makefile the directory asm will be create inside the directory /usr/src/linux-headers-4.13.0-39-generic/include/generated by Makefile+kbuild... That means that waht I told you is useless and I can't help you. Sorry.

Last edited by Delcaran; 04-24-2018 at 05:23 AM. Reason: added info
 
Old 04-24-2018, 06:57 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
If you modify a single file, make should detect it and recompile only the necessary modules ... which should include perhaps not only the file that you modified but any others that are dependent on it. Then it should perform the remaining steps needed. You should not have to – and, I think, should not – individually run the compiler.
 
Old 04-26-2018, 02:06 AM   #6
Delcaran
Member
 
Registered: Dec 2013
Location: ud.fvg.it
Distribution: Slackware64 14.2
Posts: 44

Rep: Reputation: 27
Quote:
Originally Posted by alkhimey View Post
I am developing kernel module
As I understood, OP wants to compile a source file, so unless the Makefile contains some globbing instruction he needs to modify the Makefile or equivalent...
 
  


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
Need program: compile jpeg into single file, like a book, and maintain quality H_TeXMeX_H Linux - Software 9 04-01-2010 06:29 AM
help me to check whether this is the correct way to compile c source to library ... alred Programming 0 04-29-2006 04:22 AM
single module compile in clean source tree henryquinn Linux - Software 3 02-15-2006 03:38 AM
Compiling a single source file: hardstyleFX Programming 7 01-12-2006 06:09 AM
slackware compile flags sh1ft Slackware 3 12-17-2004 01:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 03:36 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