Find the answer to your Linux question:
Results 1 to 4 of 4
Hi all, I have an object file foo.o which provide a function foo(). when I use this object file with simple main.c as shown below it works correctly. Code: #main.c ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2012
    Posts
    7

    Adding a new object file to kernel source code and use it's function


    Hi all,

    I have an object file foo.o which provide a function foo().
    when I use this object file with simple main.c as shown below it works correctly.
    Code:
    #main.c
    include "foo.h"
    
    int main()
    {
            foo();
            return 0;
    }
    I use gcc for compile & link.
    Code:
    gcc -o foo foo.o main.c
    Goal:
    I want to use foo() in init/main.c of my linux-2.6.35 kernel source code.

    What I've done:
    I added foo.o and foo.h to the kernel/init/ directory, modified my init/main.c to use foo(), and added foo.o to obj-y in init/Makefile as shown below.
    Code:
    obj-y := main.o version.o mounts.o foo.o
    Problem:
    I get these errors when I make the kernel..
    Code:
    init/built-in.o: In function `foo1':
    foo.c:(.text+0xe3): undefined reference to `printf'
    foo.c:(.text+0xf8): undefined reference to `printf'
    foo.c:(.text+0x19f): undefined reference to `printf'
    foo.c:(.text+0x1b8): undefined reference to `printf'
    init/built-in.o: In function `foo2':
    foo.c:(.text+0x1e6): undefined reference to `printf'
    init/built-in.o:foo.c:(.text+0x1ff): more undefined references to `printf' follow
    init/built-in.o: In function `foo3':
    foo.c:(.text+0xcb0): undefined reference to `putchar'
    foo.c:(.text+0xe29): undefined reference to `stdout'
    foo.c:(.text+0xe3c): undefined reference to `fputc'
    foo.c:(.text+0xe51): undefined reference to `stdout'
    foo.c:(.text+0xe63): undefined reference to `fputc'
    foo.c:(.text+0xe76): undefined reference to `puts'
    .
    .
    .
    I know the problem arises from Makefile, but I don't know how to solve it.
    I would appreciate any help!
    Thanks!

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,572
    You have some studying to do. Writing kernel code is NOT the same as normal user code, which is what you have written. There are documents and books online that will help, but you have a steep learning curve to overcome before you can do this effectively. Note that the standard library functions as printf, fputc, and such DO NOT WORK with kernel code!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Apr 2012
    Posts
    7
    Thank you Rubberman for your answer.
    How can I convert some code to work with kernel code?? can you offer me some documents or books to do this?

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,572
    Available in ebook form, and in many cases for free online:

    Linux Kernel Development, Author: Robert Love, Publisher: Addison-Wesley
    Linux Device Drivers, Author: Jonathan Corbet, Publisher: O'Reilly

    Google searches should find them PDQ.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •