Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile for beginners

I just started learning about makefiles: i created a simple tutorial by myself but it seems I am mistaken somewhere and i don't know where; my mini-tutorial is formed by a main.c script that recalls a function named kernel.c; within this last function 2 more functions are called: add.c (which adds 2 numbers together) and mul.c (which multiplies the result of the previous sum); i then created the headers kernel.h and functions.h which contain the prototypes of the functions defined above; this two header are contained in a folder created within the same one of the main.c script: common/inc

Here are the files:

//main
#include <stdio.h>
#include <stdlib.h>
#include "kernel.h"

int main(){
int a = 5, b = 4, c = 0;
int *pa, *pb, *pc;
pa = &a; pb = &b; pc = &c;

kernel(pa, pb, pc);

printf("c = %d\n", c);

return 0;
}

here is kernel.c

#include "kernel.h"
#include "functions.h"

void kernel(int* a, int*b, int* c){
 int x = add(*a,*b);
 *c = mul(x);
}

here is add.c

#include "functions.h"

int add(int a, int b){
return a + b;
}

here is mul.c

#include "functions.h"

int mul(int a){
return a*2;
}

here is kernel.h

void kernel(int* a, int*b, int* c);

here is functions.h

int add(int a, int b);
int mul(int a);

The make file i wrote looks like this:

#===========================Makefile================================#

CC=gcc
IDIR = common/inc
CFLAGS=-c -Wall -I$(IDIR)

all: eseguibile

eseguibile: main.o  
    $(CC) $(CFLAGS) main.o -o eseguibile

main.o: main.c kernel.o
    $(CC) $(CFLAGS) main.c kernel.o

kernel.o: kernel.c add.o mul.o
    $(CC) $(CFLAGS) kernel.c add.o mul.o

add.o: add.c 
    $(CC) $(CFLAGS) add.c

mul.o: mul.c 
    $(CC) $(CFLAGS) mul.c

clean:
    rm -rf *o eseguibile

I know the program works because if i type gcc main.c kernel.c add.c mul.c -I common/inc/ in the terminal everything works fine.

Can anyone tell me what I a doing wrong?`

The error that i get is this:

gcc -c -Wall -Icommon/inc add.c
gcc -c -Wall -Icommon/inc mul.c
gcc -c -Wall -Icommon/inc kernel.c add.o mul.o
gcc: warning: add.o: linker input file unused because linking not done
gcc: warning: mul.o: linker input file unused because linking not done
gcc -c -Wall -Icommon/inc main.c kernel.o
gcc: warning: kernel.o: linker input file unused because linking not done
gcc -c -Wall -Icommon/inc main.o -o eseguibile
gcc: warning: main.o: linker input file unused because linking not done
like image 865
Federico Gentile Avatar asked Oct 22 '25 12:10

Federico Gentile


1 Answers

I prefer to build the objects separately, and then combine them all at the end. I think the problem is that you are including a -c flag on the final executable. Try this:

#===========================Makefile================================#

CC=gcc
IDIR = common/inc
CFLAGS=-Wall -I$(IDIR)
OBJS = main.o kernel.o add.o mul.o

all: eseguibile

eseguibile: $(OBJS)
    $(CC) $(CFLAGS) $^ -o eseguibile

main.o: main.c kernel.h
    $(CC) -c $(CFLAGS) main.c

kernel.o: kernel.c kernel.h functions.h
    $(CC) -c $(CFLAGS) kernel.c

add.o: add.c functions.h
    $(CC) -c $(CFLAGS) add.c

mul.o: mul.c functions.h
    $(CC) -c $(CFLAGS) mul.c

clean:
    rm -rf *.o eseguibile
like image 96
Gillespie Avatar answered Oct 25 '25 01:10

Gillespie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!