Find the answer to your Linux question:
Results 1 to 6 of 6
i want to compile 3 file like this : a.c & b.c & d.h a.c is: #include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_INFO ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2011
    Posts
    7

    Post compile 2 dependent file a.c and b.c with a header d.h


    i want to compile 3 file like this : a.c & b.c & d.h
    a.c is:
    #include <linux/module.h> /* Needed by all modules */
    #include <linux/kernel.h> /* Needed for KERN_INFO */
    #include "ALU.h"
    int init_module(void)
    {
    int a=2,b=3;
    printk(KERN_INFO "add example a=%d" b1_module(a,b));
    return 0;
    }

    void cleanup_module(void)
    {
    printk(KERN_INFO "Goodbye world 1.\n");
    }
    //-----------------------------------------------------------------------------
    b.c:
    #include "d.h"
    static int b1_module( int a , int b )
    {
    .....
    return R;
    }
    //----------------------------------------------------------------------------
    d.h:
    #ifndef __d_H__
    #define __d_H__
    //---------------------------------------------------------------------------------
    //#include <linux/module.h>
    static int b1_module(int a , int b);

    #endif // __d_H__
    how can i write makefile for this or compile anywhy?
    My Linux is 2.6.

  2. #2
    Linux Enthusiast Bemk's Avatar
    Join Date
    Sep 2008
    Location
    Oosterhout-NB, Netherlands
    Posts
    525
    gcc -c will probably do the trick for the object files, though you might want to include a -I<path/to/include> option in there.

    As for the linker, I don't know what type of binary image you need. Is it relocatable or flat binary? man ld might get you started there, and maybe look at the way the Linux kernel compiles things. It has taught me a lot for my own home made kernel. Although I've been looking at Linux-0.0.1
    Full time computer science student, spare time OS developer.
    @bemk92 on twitter.

  3. #3
    Just Joined!
    Join Date
    Jul 2011
    Posts
    2
    gcc -I/<path of d.h> -o executable a.c b.c

  4. #4
    Linux Enthusiast Bemk's Avatar
    Join Date
    Sep 2008
    Location
    Oosterhout-NB, Netherlands
    Posts
    525
    Quote Originally Posted by manik019 View Post
    gcc -I/<path of d.h> -o executable a.c b.c
    This works, but only if the files aren't dependent of each other.

    Code:
    gcc -c {a,b}.c
    ld -R <any necessary other options> -o <output name>
    This'll make the code relocatable, and might just be better when dependent of symbols defined in the core binary (could be the linux kernel image, but also something you might link into).
    Full time computer science student, spare time OS developer.
    @bemk92 on twitter.

  5. #5
    Just Joined!
    Join Date
    Jul 2011
    Posts
    7
    thanks a lot i have solved this problem with a makefile that introduce kernel headers i have to compile the kernel for this porpuse -I/include path and -d __KERNEL

  6. #6
    Just Joined!
    Join Date
    Jul 2011
    Posts
    16
    Use the following makefile, no need to compile the kernel.
    vim Makefile

    obj-m := test.o
    test-objs := a.o b.o

    KDIR=/lib/modules/$(shell uname -r)/build

    all:
    make -C $(KDIR) M=$(PWD) modules
    Last edited by MikeTbob; 07-30-2011 at 07:49 PM. Reason: Deleted advertising

Posting Permissions

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