简体   繁体   English

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

[英]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 . 我有4个.c文件hello.chere.cbye.cmain.c One header file mylib.h 一个头文件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 main.c

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

int main()
{ 

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

mylib.h mylib.h

#ifndef _mylib_
#define _mylib_

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

#endif

The makefile for creating a static lib is : Makefile 用于创建静态库的makefile是: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.o'
make: *** [(hello.o)] Error 2

Questions : How do I resolve this? Which command is causing the cyclic dependency? 问题: How do I resolve this? Which command is causing the cyclic dependency? 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... 如果我正确地记住我的makefile语法,则该行显示$(MYLIB) depends on $(MYLIB)...当然,计算结果为: mylib.a: mylib.a...

Drop the extraneous $(MYLIB) in the dependency list. 将无关的$(MYLIB)放到依赖项列表中。 That is: 那是:

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

should be: 应该:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM