Help with makefile

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 817 Views - Last Post: 23 August 2011 - 04:39 AM Rate Topic: -----

#1 gigamico  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-July 11

Help with makefile

Posted 05 August 2011 - 11:13 AM

Hi,
I have this code:
#include <linux/kernel.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
int init_module(void) //entry point
{
printk("Hello world!\n"); // printk = scrive in /var/log/messages
return 0;
}
void cleanup_module(void)//exit point
{
printk("Goodbye world!\n");
return;
}



I have to compile the code with this makefile
EXTRA_CFLAGS += -I/usr/realtime/include -D_IN_RTAI_
obj-m += prog1.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


What I have understood is that I have to compile the hello world file as an object, but I don't know how to use the makefile. Could someone please explain to me what to do?

This post has been edited by gigamico: 05 August 2011 - 11:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with makefile

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,357
  • Joined: 31-December 10

Re: Help with makefile

Posted 05 August 2011 - 11:24 AM

Typically Makefiles will be called on the command-line inside the directory they reside. So open the terminal and move to the directory where your project is, and just type
make
, hit enter, and if the Makefile and your source file don't have any errors, then it should be compiled correctly.
Was This Post Helpful? 1
  • +
  • -

#3 gigamico  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-July 11

Re: Help with makefile

Posted 06 August 2011 - 12:46 AM

Thanks, now it works. :)
I've created a new folder, and I moved the two files inside it.
At the beginning it did not found the makefile because I saved it as .txt, but after deleting the extension it works great!

Please don't close this thread, maybe I will need your help again.
Was This Post Helpful? 0
  • +
  • -

#4 gigamico  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-July 11

Re: Help with makefile

Posted 06 August 2011 - 09:33 AM

now I'm trying to compile this code, this is the prog1.c file
#include <linux/kernel.h>    
#include <linux/module.h>    
#include <linux/version.h>   
#include <linux/errno.h>    

#include "rtai.h"        
#include "rtai_sched.h"        
#include <rtai_sem.h>

MODULE_LICENSE("GPL");

static RT_TASK print_task;   

long long int t0,t1,t2;

void print_function(long arg)     
{
   rt_busy_sleep(1000000);

   t0=rt_get_time_ns();
   rt_printk("ora  t0(ns) %lld inizio t1 (ns) %lld\n",t0,t1);
   rt_printk("differenza %lld (ns)\n", (t0-t1));
   return;
}

int init_module(void)
{
    int retval;            
    long long int delay; //50 ms

    rt_set_oneshot_mode();

    start_rt_timer(0);



    retval =  rt_task_init(&print_task, print_function, 0, 1024, RT_SCHED_LOWEST_PRIORITY, 0, 0);
    if (0 != retval) {
        if (-EINVAL == retval) {
            printk("task: invalida struttura dei task\n");
        } else {
            printk("task: errore \n");
        }
        return retval;
    }


   delay=5000000;

    t1=rt_get_time_ns();
    rt_printk("initmodule - tempo corrente t1 %lld\n",t1); //rt_ 


//    retval = rt_task_resume(&print_task); 
    retval = rt_task_make_periodic(&print_task, rt_get_time()+5000000, 100000); 
    if (0 != retval) {
        if (-EINVAL == retval) {
            printk("task: invalida struttura dei task\n");
        } else {
            printk("task: errore\n");
        }
        return retval;
    }


    return 0;
}

void cleanup_module(void)
{
  return;
}



using the same makefile as before, the compiler gives these warnings
Posted Image

the rt_ functions seem to create problems, so I suppose it is because the compiler does not find their definitions.
the directory
/usr/realtime/include
should hold the definitions
Posted Image
Posted Image

in fact, the first line of the makefile should indicate that directory
EXTRA_CFLAGS += -I/usr/realtime/include -D_IN_RTAI_


but why it doesn't work? any ideas?
I'm a very beginner, so it could be a trivial mistake, but I don't get it...
Was This Post Helpful? 0
  • +
  • -

#5 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,357
  • Joined: 31-December 10

Re: Help with makefile

Posted 06 August 2011 - 10:48 AM

You can't use the same Makefile for every source file you create. Each time you make a new source file, you need to make or add that file to the Makefile. Also, the first line of you Makefile just makes a variable that can be used later on, but the line where you're actually compiling doesn't use the "EXTRA_CFLAGS", so to add that to the line, put this after "make -C", $(EXTRA_CFLAGS) and see if that helps.
Was This Post Helpful? 0
  • +
  • -

#6 gigamico  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-July 11

Re: Help with makefile

Posted 07 August 2011 - 01:38 AM

No, it does not work. :(

The website that holds all the source codes that I'm trying to compile has only one makefile, so I suppose that this makefile is general.

ftp://docenti.ing.un...Programmazione/
Was This Post Helpful? 0
  • +
  • -

#7 gigamico  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-July 11

Re: Help with makefile

Posted 07 August 2011 - 01:56 AM

This is what the compiler says when I add $(EXTRA_FLAGS)
Posted Image
Was This Post Helpful? 0
  • +
  • -

#8 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,357
  • Joined: 31-December 10

Re: Help with makefile

Posted 07 August 2011 - 09:07 AM

Makefiles aren't general so that they will compile different source files. Makefiles hold rules to compile source code, when you add new files, you need to add a new rule to compile/link that source file into the program.
Was This Post Helpful? 0
  • +
  • -

#9 gigamico  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-July 11

Re: Help with makefile

Posted 07 August 2011 - 10:48 AM

Ok..
I am not very practical with makefiles, so could you show me how to do, for example, to compile the code on post #4?

There is something else that you need to know?

This post has been edited by gigamico: 07 August 2011 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#10 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,078
  • Joined: 01-December 05

Re: Help with makefile

Posted 07 August 2011 - 02:21 PM

Makefiles can be made very general. There are normally default rules for C/C++ programs.
#This makefile will compile all C source files into similarly named executables
#file.c -> file ; test.c -> test
#You don't even need to specify targets
CC=/usr/bin/gcc
CFLAGS=-W -Wall -Wextra -pedantic


When working on a large project, having each source file compile to an object file is standard. There are two general purpose ways to do this:
#Make use of the wildcard matching
CC=/usr/bin/gcc
CFLAGS=-W -Wall -Wextra -pedantic

%.o: %.c
    $(CC) $(CFLAGS) $^ -o $@


#Use the .suffixes default rule
CC=/usr/bin/gcc
CFLAGS=-W -Wall -Wextra -pedantic

.c.o:
    $(CC) $(CFLAGS) $^ -o $@



You absolutely do not need to have a single file generated per rule. Depending on how you set up your makefile, you can have a few generic rules, such as the %.o: %.c, and if you separate your various libraries into different directories, you can have a single rule to create libraries from an arbitrary amount of object files as well.
CC = /usr/bin/gcc
CFLAGS = -W -Wall -Wextra -pedantic -I.
LDFLAGS = -L. -lone -ltwo -lthree
LIBS = libone.a libtwo.a libthree.a
SRCS = $(wildcard $(LIBS:lib%.a=%/*.c)) main.c
OBJS = $(SRCS:%.c=%.o)

.PHONY: clean all
.SUFFIXES:

all:
        $(MAKE) $(OBJS)
        $(MAKE) main

main: main.o $(LIBS)
        $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

lib%.a: %/
        ar rcs $@ $</*.o

%.o: %.c
        $(CC) $(CFLAGS) -c -o $@ $^

clean:
        rm -Rf $(LIBS) $(OBJS) main



A single Makefile is most likely not suitable for a large project, but with knowledge of autovariables, special rules, and special functions you can minimize the amount of modifications you have to perform on a makefile.
Was This Post Helpful? 1
  • +
  • -

#11 gigamico  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-July 11

Re: Help with makefile

Posted 08 August 2011 - 12:49 AM

Thanks for the reply,
but it didn't help me much.

For now forget about the makefile, I have only to compile a single file as an object.

The program that I want to compile is the one on post #4, first of all, how do I tell the compiler to read the functions definitions?
Was This Post Helpful? 0
  • +
  • -

#12 gigamico  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-July 11

Re: Help with makefile

Posted 08 August 2011 - 01:41 AM

Actually, I have to compile the file as a kernel object, with the .ko extension
Was This Post Helpful? 0
  • +
  • -

#13 gigamico  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-July 11

Re: Help with makefile

Posted 13 August 2011 - 01:46 AM

up
Was This Post Helpful? 0
  • +
  • -

#14 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,357
  • Joined: 31-December 10

Re: Help with makefile

Posted 13 August 2011 - 05:55 AM

Did you try google?
Was This Post Helpful? 0
  • +
  • -

#15 gigamico  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-July 11

Re: Help with makefile

Posted 13 August 2011 - 06:12 AM

Yes, but I didn't find anything helpful...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2