Search code examples
cmakefile

A simple makefile in C - Object file with different name from C file


I'm trying to create a makefile for my program (a very simple one). My files are lab.c and lab.h. I need to create object file of lab.c and with a different name (matlib.o), and compile without linkage.

This is my attempt:

matlib: matlib.o lab.o
    gcc -c matlib.o
matlib.o: lab.c lab.h
    gcc -c lab.c

However, I'm getting this error:

 No rule to make target 'matlib.o', needed by 'matlib'. Stop.

What am I doing wrong?


Solution

  • You should tell gcc to output the object file in a different name with -o flag.

    The correct recipe is

    matlib: matlib.o
        gcc matlib.o -o matlib
    matlib.o: lab.c lab.h
        gcc -c lab.c -o matlib.o