gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:49:52 25 4
gpt4 key购买 nike

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

内容如下

你好.c

#include<stdio.h>

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

这里.c

#include<stdio.h>

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

再见

#include<stdio.h>

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

主程序

#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

问题:我该如何解决这个问题?哪个命令导致循环依赖?

最佳答案

#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

没有括号。

关于c - 在用于创建静态库的 makefile 中找到递归依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3945650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com