简体   繁体   English

如何在使用 g++ 和 gcc 编译 obj 文件的 Makefile 中避免循环依赖?

[英]How can I avoid circular dependency in my Makefile compiling obj file with g++ and gcc?

I tried to create a makefile for a project that use c++ and c.我试图为使用 c++ 和 c 的项目创建一个 makefile。 I need to compile those file in order to make de .o file, but when I compile using make I have circular dependency that is dropped.我需要编译这些文件才能制作 de .o 文件,但是当我使用 make 编译时,我会删除循环依赖项。

I don't know why this error occurs as I tried to separate the building of .o files that comes from .c and .o files that comes froms .cpp files.我不知道为什么会发生此错误,因为我试图将来自 .c 的 .o 文件的构建和来自 .cpp 文件的 .o 文件分开。

Here is my makefile :这是我的生成文件:

SRC = $(wildcard *.c)
SRC++ = $(wildcard *.cpp)
OBJ = $(SRC++:.cpp=.o) $(SRC:.c=.o)
DEP = $(SRC:.c=.d) $(SRC++:.cpp=.d)

CC=gcc
CXX=g++

CFLAGS = -Wall
CXXFLAGS = -Wall

all: $(OBJ)

%.c : %.o
    $(CC) -c $(CFLAGS) $< -o $@  

%.cpp : %.o
    $(CXX) -c $(CXXFLAGS) $< -o $@  

# Clean the project
clean:
    @rm -f $(OBJ) $(DEP)

-include $(DEP)

Do you have any idea of what I am doing wrong ?你知道我做错了什么吗?

The rules you wrote are %.cpp : %.o and %.c : %.o .您编写的规则是%.cpp : %.o%.c : %.o You wrote those rules the wrong way around.您以错误的方式编写了这些规则。 The target being built must be to the left of the colon, and the things it depends on must be to the right.正在构建的目标必须在冒号的左边,它所依赖的东西必须在冒号的右边。

The error message tells you about a circular dependency because GNU Make defines an implicit rule where the dependencies are defined in the correct direction.错误消息告诉您有关循环依赖的信息,因为 GNU Make 定义了一个隐式规则,其中在正确方向上定义了依赖关系。

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

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