在makefile中找到的用于创建静态库的递归依赖项

时间:2010-10-15 19:56:19

标签: c linux makefile static-libraries

我有4个.c文件hello.chere.cbye.cmain.c。 一个头文件mylib.h

内容如下

的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

用于创建静态库的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

问题:当我执行命令

make -f Makefile all 

我收到以下依赖性错误:

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.o'
make: *** [(hello.o)] Error 2

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

3 个答案:

答案 0 :(得分:3)

#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)

看起来最后一条规则是

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

这是循环依赖。

该行应为

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

没有括号。

答案 1 :(得分:2)

不是积极的,但是:

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

如果我正确记住了我的makefile语法,该行说$(MYLIB) depends on $(MYLIB)...当然评估为:mylib.a: mylib.a...

答案 2 :(得分:1)

将无关的$(MYLIB)放入依赖关系列表中。那就是:

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

应该是:

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