gpt4 book ai didi

c++ - 在 makefile 中添加源文件和更新依赖项

转载 作者:行者123 更新时间:2023-11-30 03:29:15 24 4
gpt4 key购买 nike

我正在用 C++ 编写一个(动态)库,并对其进行了一组测试。目前,我正在制作一个 makefile(默认目标),它可以构建库或构建并运行测试(test 目标)。整个区别在于几个链接器标志以及是否包含单个 .cpp 文件(包含 main),但我无法获取 test 目标以正确构建,因为依赖列表由于某种原因没有得到更新。这是整个生成文件:

CXX = g++
RM=rm -f

EXTRA_CCFLAGS = -Wall -Wextra -Wcast-qual -Wfloat-equal -Wpedantic -Wpointer-arith -Wshadow
CXXOPTFLAGS = -O2
CXXFLAGS = -std=c++11 $(EXTRA_CCFLAGS) $(CXXOPTFLAGS)

LDLIBS=-larmadillo
MAKELIBFLAGS=-current_version 0.1 -compatibility_version 0.1 -dynamiclib

TARGET = libadsp.dylib
TESTTARGET = dsptest
SOURCE_FILES=src/stft.cpp src/filters.cpp src/windows.cpp src/helpers.cpp
OBJECT_FILES=$(subst .cpp,.o,$(SOURCE_FILES))

all: $(TARGET)

test: SOURCE_FILES += tests/tests.cpp
test: OBJECT_FILES = $(subst .cpp,.o,$(SOURCE_FILES))
test: $(TESTTARGET)

$(TARGET): $(OBJECT_FILES)
$(CXX) $(CXXFLAGS) $(MAKELIBFLAGS) -o $(TARGET) $(OBJECT_FILES) $(LDLIBS)

$(TESTTARGET): $(OBJECT_FILES)
$(CXX) $(CXXFLAGS) -o $(TESTTARGET) $(OBJECT_FILES) $(LDLIBS)
./dsptest

depend: .depend

.depend: $(SOURCE_FILES)
$(RM) ./.depend
$(CXX) $(CXXFLAGS) -MM $^>>./.depend;

.PHONY: clean

clean:
$(RM) $(OBJECT_FILES)
$(RM) ./.depend

include .depend

最佳答案

您不能在先决条件列表中使用特定于目标的变量值。 GNU make 手册是 very clear关于这个:

The other exception is target-specific variable values. This feature allows you to define different values for the same variable, based on the target that make is currently building. As with automatic variables, these values are only available within the context of a target’s recipe (and in other target-specific assignments).

(强调)。您可以专门添加测试目标:

$(TESTTARGET): $(OBJECT_FILES) tests/tests.o
$(CXX) $(CXXFLAGS) -o $@ $^ $(LDLIBS)
./dsptest

附言。我不推荐使用 $(subst ...) 来替换扩展;使用 $(patsubst ...) 代替。

关于c++ - 在 makefile 中添加源文件和更新依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45841544/

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