开发者

makefile fails to correctly generate "all" target

开发者 https://www.devze.com 2023-03-07 20:42 出处:网络
I\'ve been working on a makefile with parametrized targets. On my OpenSUSE PC, it runs successfully when given make all, but (strangely) runs a clean instead of a make all if you only provide make wit

I've been working on a makefile with parametrized targets. On my OpenSUSE PC, it runs successfully when given make all, but (strangely) runs a clean instead of a make all if you only provide make with no extra arguments. Worse, on my Ubuntu PC, make all doesn't work at all, and from browsing throu开发者_StackOverflowgh make -pn all | less, all isn't even being generated as a target.

Clearly I'm doing something wrong when generating the all target. Here is the makefile:

# To see predefined preprocessor macros:
# gcc -dM -E - < /dev/null | sort | less

# After a default installation of opencc, you might need to run:
# sudo ln -s /opt/x86_open64-4.2.5/lib/gcc-lib /usr/lib/gcc-lib

allflags = -Wall -pipe
cflags   = -std=c99
cxxflags =

flags_O0 = -O0 -ggdb
flags_O1 = -O1
flags_O2 = -O2
flags_O3 = -O3 -fomit-frame-pointer

flags_opt_gcc    = -march=native -s
flags_opt_icc    = -march=native -s
flags_opt_opencc = -march=auto -s
flags_opt_clang  = -march=native

ldflags_O0 = $(cflags_O0)
ldflags_O1 = $(cflags_O1) -Wl,-s -Wl,-O,1
ldflags_O2 = $(cflags_O2) -Wl,-s -Wl,-O,2
ldflags_O3 = $(cflags_O3) -Wl,-s -Wl,-O,3

define dcompiler =
version-$(1):
    $(1) --version 2>&1 | sed 's/.*/"&\\n"/' > $$@
flags_$(1)_O0 = $$(allflags) $$(flags_O0) $$(flags_$(1))
flags_$(1)_O1 = $$(allflags) $$(flags_O1) $$(flags_$(1)) $$(flags_opt_$(1))
flags_$(1)_O2 = $$(allflags) $$(flags_O2) $$(flags_$(1)) $$(flags_opt_$(1))
flags_$(1)_O3 = $$(allflags) $$(flags_O3) $$(flags_$(1)) $$(flags_opt_$(1))
$$(eval $$(call diter,$(1),O0))
$$(eval $$(call diter,$(1),O1))
$$(eval $$(call diter,$(1),O2))
$$(eval $$(call diter,$(1),O3))
endef

define dimpaler =
cc_impaler_$(2) = gcc $$(flags_gcc_$(2)) $$(cflags)
all: impaler$(1)
impaler$(1): impaler$(1).o util$(1).o
    $$(cc_impaler_$(2)) -o $$@ $$^ $$(ldflags_$(2)) -ldl -lm -lpopt
impaler$(1).o: impaler.c impaler.h util.h makefile
    $$(cc_impaler_$(2)) -o $$@ $$< -c
util$(1).o: util.c util.h makefile
    $$(cc_impaler_$(2)) -o $$@ $$< -c
clean::
    rm -f impaler$(1)
endef

define diter =
cc_iters_$(1)_$(2) = $(1) $$(flags_$(1)_$(2))
all: iters-$(1)-$(2).so
iters-$(1)-$(2).so: iters-$(1)-$(2).c.o iters-$(1)-$(2).cpp.o
    $$(cc_iters_$(1)_$(2)) -o $$@ $$^ -shared $$(ldflags_$(2))
iters-$(1)-$(2).c.o: iters.c impaler.h version-$(1) makefile
    $$(cc_iters_$(1)_$(2)) $$(cflags) -o $$@ $$< -c
iters-$(1)-$(2).cpp.o: iters.cpp impaler.h version-$(1) makefile
    $$(cc_iters_$(1)_$(2)) $$(cxxflags) -o $$@ $$< -c
endef

clean::
    rm -f *.o *.so version-*

$(eval $(call dimpaler,-debug,O0))
$(eval $(call dimpaler,,O3))
$(eval $(call dcompiler,gcc))
$(eval $(call dcompiler,icc))
$(eval $(call dcompiler,opencc))
$(eval $(call dcompiler,clang))

Any hints would be appreciated. Thanks.


Edit:

Even when I reduce the makefile to the following:

define dimpaler =
all: impaler
impaler: impaler.o
    gcc -o $$@ $$^
impaler.o: impaler.c
    gcc -o $$@ $$< -c
endef

$(eval $(call dimpaler))

"all" is still not generated.


Edit:

Contrary to the very clearly explained syntax in http://www.gnu.org/software/make/manual/make.html#Reading-Makefiles , apparently adding an = after a define makes everything silently fail, and omitting the = makes the makefile behave better.

Now I'm left with the other problem: make all works, but make doesn't.


As you said, removing the = after the define makes it work better. It may be a change in how the = is handled in define.

As for the other problem, make doesn't work because all is not defined as the first rule. Try adding an empty all: rule at the beginning. There should be also a way of establishing the default target rule, but I'll look into the documentation as I don't remember it right now.

Edit:

Seems to be the variable .DEFAULT_TARGET.

0

精彩评论

暂无评论...
验证码 换一张
取 消