Skip to content
Advertisement

Makefile, add headers located in parent directory (Not allowed to move inside!)

dir1
--- myheader1.h
--- myheader2.h
    src
    --- prog.c
    --- makefile

prog.c

#include <stdio.h>
#include "myheader1.h"
#include "myheader2.h"

int main()
{
    func1();
    func2();
    return 0;
}

myheader1.h

void func1()
{
    printf("Header1n");
}

myheader2.h

void func2()
{
    printf("Header2n");
}

Looking for solution on how to add a header from parent directory I found this question. But it didn’t work for me and I am still getting error of not finding the header file.

No rule to make target ‘myheader1.h’, needed by ‘prog.o’. Stop.

makefile

CFLAGS = -c -Wall -I.. myheader1.h

prog : prog.o
     cc -o prog prog.o

prog.o : prog.c myheader1.h
     cc $(CFLAGS) prog.c

What am I doing incorrect? and how to add the second header without making lines go extremely long or ugly?

Advertisement

Answer

prog.o : prog.c myheader1.h

This line says prog.o depends on two files, prog.c and myheader1.h. prog.c exists, but myheader1.h does not (at least not in the same directory).

It should be

CFLAGS = -Wall -I..

prog.o : prog.c ../myheader1.h
        cc $(CFLAGS) -c prog.c

Note:

  • ../myheader1.h in the list of prerequisites, not myheader1.h
  • headers should not be listed on the compiler command line at all
  • -c doesn’t really belong in CFLAGS
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement