开发者

Recursive dependency found in a makefile for creating a static library

I have 4 .c files hello.c,here.c,bye.c and main.c. One header file mylib.h

The contents are as follows

hello.c

#include<stdio.h>

void hello()
{
    printf("Hello!\n");
}

here.c

#include<stdio.h>

void here()
{
     printf("I am here \n");
}

bye.c

#include<stdio.h>

void bye()
{
    printf("Bye,Bye");
}

main.c

#include<stdio.h>
#include "mylib.h"

int main()
{ 

  hello();
  here();
  bye();
  return 1;
}

mylib.h

#ifndef _mylib_
#define _mylib_

void hello();
void here();
void bye();

#endif

The makefile for creating a static lib is : Makefile

all:    myapp

#Macros

#Which Compiler
CC = g++

#Where to install
INSTDIR = /usr/local/bin

#Where are include files kept
INCLUDE = .

#Options for developement
CFLAGS = -g -Wall -ansi

#Options for release
#CFLAGS = -O -Wall -ansi

#Local Libraries
MYLIB = mylib.a

myapp:  main.o $(MYLIB)
    $(CC) -o myapp main.o $(MYLIB)

$(MYLIB):       $(MYLIB)(hello.o) $(MYLIB)(here.o) $(MYLIB)(bye.o)
main.o:         main.c mylib.h
hello.o:        hello.c
here.o:         here.c
bye.o:          bye.c

clean:
    -rm main.o hello.o here.o bye.o $(MYLIB)

install:        myapp
    @if [ -d $(INSTDIR) ]; \
    then \
            cp myapp $(INSTDIR);\
            chmod a+x $(INSTDIR)/myapp;\
            chmod og-w $(INSTDIR)/myapp;\
            echo "Installed in $(INSTDIR)";\
    else \
            echo "Sorry, $(INSTDIR) does not exist";\
    fi

Problem: When I execute the command

make -f Makefile all 

I get the following dependecy error:

make: Circular mylib.a <- mylib.a dependency dropped.
ar rv (hello.o) hello.o
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `ar rv (hello.o) hello.开发者_JS百科o'
make: *** [(hello.o)] Error 2

Questions : How do I resolve this? Which command is causing the cyclic dependency?


#Local Libraries
MYLIB = mylib.a

myapp:  main.o $(MYLIB)
    $(CC) -o myapp main.o $(MYLIB)

$(MYLIB):       $(MYLIB)(hello.o) $(MYLIB)(here.o) $(MYLIB)(bye.o)

It looks like the last rule is

mylib.a: mylib.a (hello.o) mylib.a (here.o) mylib.a (bye.o)

Which is a circular dependency.

The line should be

mylib.a: hello.o here.o bye.o

Without the parentheses.


Not positive, but:

$(MYLIB):       $(MYLIB)(hello.o) $(MYLIB)(here.o) $(MYLIB)(bye.o)

If I remember my makefile syntax correctly, that line says $(MYLIB) depends on $(MYLIB)... Which of course evaluates to: mylib.a: mylib.a...


Drop the extraneous $(MYLIB) in the dependency list. That is:

$(MYLIB):       $(MYLIB)(hello.o) $(MYLIB)(here.o) $(MYLIB)(bye.o)

should be:

$(MYLIB):       hello.o here.o bye.o
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜