make: *** [Makefile:59: run] Error 1
andy27182 opened this issue · comments
andy27182 commented
hi,初学makefile,感谢你的视频,收货很多,项目已star。
但是当我尝试自己完成例程的时候,遇到了一个小问题,最终执行 make run 可以正确的编译出结果并执行,但是 printf 之后总是会有一行错误:make: *** [Makefile:59: run] Error 1,下面是我的 makefile 文件,使用参数 LIBT=d 选择编译动态库。
cpp_src := $(shell find src -name *.cpp)
lib_src := $(filter-out src/main.cpp,$(cpp_src))
main_obj := obj/main.o
lib_obj := $(lib_src:src%.cpp=obj%.o)
inc_path := $(abspath ./inc)
inc_option := $(inc_path:%=-I%)
alibs := ./lib/math.a
solibs := ./lib/math.so
ifeq ($(LIBT),d)
# dynamic libs
libs := $(basename $(notdir $(solibs)))
else
libs := $(basename $(notdir $(alibs)))
endif
lib_option := $(libs:%=-l%)
lib_path := $(abspath ./lib)
LIB_option := $(lib_path:%=-L%)
compile_options := -g -O3 -w
compile_options += $(inc_option)
ifeq ($(LIBT),d)
# dynamic libs
compile_options += -fPIC
endif
link_options := $(lib_option) $(LIB_option)
ifeq ($(LIBT),d)
# dynamic libs
link_options += -Wl,-rpath=$(lib_path)
endif
obj/%.o: src/%.cpp
@mkdir -p $(dir $@)
@g++ -c $^ -o $@ $(compile_options)
lib/libmath.a: $(lib_obj)
@mkdir -p ./lib
@ar -r $@ $^
lib/libmath.so: $(lib_obj)
@mkdir -p ./lib
@g++ -shared $^ -o $@
ifeq ($(LIBT),d)
# dynamic libs
library: lib/libmath.so
else
library: lib/libmath.a
endif
exec/exe: $(main_obj) library
@mkdir -p $(dir $@)
@g++ $< -o $@ $(link_options)
run: exec/exe
@./$<
clean:
rm -rf lib obj exec
debug:
@echo "$(lib_path)"
@echo "$(LIB_option)"
.PHONY: debug library clean run
执行情况:
(base) ***@ubuntu:~/workspace/maketest$ make run
ar: creating lib/libmath.a
a + b = 15
a - b = 5
make: *** [Makefile:59: run] Error 1
目录结构:
.
├── inc
│ ├── add.hpp
│ └── minus.hpp
├── Makefile
└── src
├── add.cpp
├── main.cpp
└── minus.cpp