Makefile make的编译器行为异常

Makefile make的编译器行为异常,makefile,Makefile,关于makefile有一个非常奇怪的问题,请让我描述一下: 我的项目中有两个文件,一个文件是makefile,其中makefile的内容如下: context:: @echo "hello makefile" all: echo "hello makefile.out" ############################################################################ # # Lic

关于makefile有一个非常奇怪的问题,请让我描述一下:

我的项目中有两个文件,一个文件是makefile,其中makefile的内容如下:

context::
    @echo "hello makefile"
all:
    echo "hello makefile.out"
############################################################################
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.  The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################/


context::
    @echo "hello makefile"
另一个是makefile.out,该makefile.out文件的内容如下:

context::
    @echo "hello makefile"
all:
    echo "hello makefile.out"
############################################################################
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.  The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################/


context::
    @echo "hello makefile"
当我要编译我的项目时,我将使用
make-f makefile.out
替换
make
操作,因为我想调用makefile.out文件,但奇怪的是,当我执行
make-f makefile.out
时,我会在控制台上看到这样的操作:

cp Makefile Makefile.out
hello makefile
然后我的makefile.out文件被修改如下:

context::
    @echo "hello makefile"
all:
    echo "hello makefile.out"
############################################################################
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.  The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################/


context::
    @echo "hello makefile"
正如你所看到的,这个结果不是我想要的,但我不知道为什么会出现这个问题


我不知道是否有人在我身上遇到过类似的问题。如果是这样,您能告诉我解决方案代码吗?

我知道为什么会发生这种情况

首先,GNU make有一个内置规则,显示如何从
%
文件中构建
%
文件<代码>制作-p将显示:

%.out: %
#  recipe to execute (built-in):
        @rm -f $@
         cp $< $@
第三,您可以通过将以下内容添加到makefile中来删除该默认规则:

%.out: %
(没有其他先决条件,没有配方)。看

第四,您可以通过向
MAKEFLAGS
添加
-r
选项来删除所有内置模式规则;将其添加到makefile中,如下所示:

MAKEFLAGS += -r

注意:此解决方案需要GNU make 4.0或更高版本。

您使用的make版本是什么?(请尝试
make-v
)您确定正在从包含您描述的文件的目录中运行
make
?我不会用GNU make 4.0复制这一点。我不准备相信任何一致的
make
实现在使用一个makefile时会表现出所描述的行为,该makefile的完整内容如问题中对两个makefile的描述。感谢您的快速回复,它完美地解决了我的问题。