简体   繁体   English

makefile循环依赖项和覆盖命令

[英]makefile circular dependency and overriding commands

Have a reference makefile that im slowly editing and using which spits out these two errors 有一个我正在慢慢编辑和使用的参考makefile,它会吐出这两个错误

Makefile:25: warning: overriding commands for target `build/semanticHash'
Makefile:21: warning: ignoring old commands for target `build/semanticHash'
make: Circular build/semanticHash <- build/semanticHash dependency dropped.
cc -g -ldl  -lgsl -lgslcblas -lzmq  -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG  -fPIC   -c -o src/semanticHash/rmb.o src/semanticHash/rmb.c

I'm new to makefile syntax and rules, so any common mistakes I Google for, but no luck for myself in this case. 我是makefile语法和规则的新手,所以我会遇到Google的任何常见错误,但是在这种情况下我自己不走运。

So the question is, where am I causing these errors, and is there any patterns that I should avoid in my current makefile? 所以问题是,我在哪里引起这些错误,在我的当前makefile中应该避免任何模式吗?

CFLAGS= -g $(LIBS) -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS)
LIBS= -ldl  $(OPTLIBS)
PREFIX?=/usr/local
OPTLIBS= -lgsl -lgslcblas -lzmq 

SOURCES=$(wildcard src/**/*.c src/*.c)
OBJECTS=$(patsubst %.c,%.o,$(SOURCES))

TEST_SRC=$(wildcard tests/*_tests.c)
TESTS=$(patsubst %.c,%,$(TEST_SRC))

TARGET=build/semanticHash  # Rename to library !!!!!
SO_TARGET=$(patsubst %.a,%.so,$(TARGET))

# The Target Build
all: $(TARGET) tests


$(TARGET): CFLAGS += -fPIC
$(TARGET): build $(OBJECTS)
    ar rcs $@ $(OBJECTS)
    ranlib $@

$(SO_TARGET): $(TARGET) $(OBJECTS) 
    $(CC) -shared -o $@ $(OBJECTS) 

build:
    @mkdir -p build
    @mkdir -p bin

# The Unit Tests
.PHONY: tests
tests: CFLAGS += $(TARGET)
tests: $(TESTS)
    sh ./tests/runtests.sh

valgrind:
    VALGRIND="valgrind --log-file=/tmp/valgrind-%p.log" $(MAKE)

# The Cleaner
clean:
    rm -rf build $(OBJECTS) $(TESTS)
    rm -f tests/tests.log 
    find . -name "*.gc*" -exec rm {} \;
    rm -rf `find . -name "*.dSYM" -print`

# The Install
install: all
    install -d $(DESTDIR)/$(PREFIX)/lib/
    install $(TARGET) $(DESTDIR)/$(PREFIX)/lib/

You set SO_TARGET to the same as TARGET : 您将SO_TARGET设置为与TARGET相同:

TARGET=build/semanticHash  # Rename to library !!!!!
SO_TARGET=$(patsubst %.a,%.so,$(TARGET))

As seen TARGET does not end with .a , so nothing will be substituted making SO_TARGET the same. 正如所看到的TARGET 不是结束.a ,所以什么都不会被取代使得SO_TARGET相同。

Later you have 以后你有

$(SO_TARGET): $(TARGET) $(OBJECTS) 

As both SO_TARGET and TARGET are the same, you have a circular dependency. 由于SO_TARGETTARGET相同,因此具有循环依赖项。

The other two warnings are because of this issue as well, as you have conflicting commands for the same target. 其他两个警告也是由于此问题,因为您对同一目标有冲突的命令。

On an unrelated note, you should not mix compiler and linker flags like you do. 无关紧要的是,您不应像您那样混合使用编译器和链接器标志。 Compiler flags are for compilation, linker flags for linking. 编译器标志用于编译,链接器标志用于链接。 You should also change the order of the linker options, and place the libraries to link with after the object files. 你也应该改变的连接选项的顺序,并把库中的目标文件链接。 This is because the GNU linker doesn't load libraries if there isn't anything depending on them, and dependencies are not loaded until it loads the object files. 这是因为如果没有任何依赖库的链接,则GNU链接器不会加载库,并且依赖项只有在加载目标文件后才会加载。

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

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