简体   繁体   English

Makefile混乱

[英]Makefile Confusion

I'm new to makefiles, but I finally managed to get one working for a project I'm working on. 我是makefile的新手,但最终我设法为正在从事的项目工作了一个文件。 The original goal was to search the folder "Source" for any CPP files, then create an Object file in the folder Products/Objs. 最初的目标是在文件夹“ Source”中搜索所有CPP文件,然后在Products / Objs文件夹中创建一个Object文件。 Instead, it finds the CPP files, but OBJECT_FILES is the same as SOURCE_FILES, and I'm not sure how it's working. 相反,它找到了CPP文件,但是OBJECT_FILES与SOURCE_FILES相同,我不确定它是如何工作的。 It doesn't generate any .o files (that I could see), but still compiles successfully. 它不会生成任何.o文件(我可以看到),但是仍然可以成功编译。 Super confused but would love some help. 超级困惑,但希望得到一些帮助。 Thanks! 谢谢!

##########################################
#           Editable options             #
##########################################

# Program Name
EXECUTABLE=net

# Compiler options
CC=g++
CFLAGS=
LDFLAGS=

# Folders
SRC=Source
OBJ=Products/Objs

#########################################################
#                     Do Not Touch This                 #
#########################################################
SOURCE_FILES := $(shell find $(SRC)/ -type f -name '*.cpp')

## This should not work
OBJECT_FILES := $(SOURCE_FILES: $(SRC)/%.cpp=%.o)
## Like at all --> It's the same as SOURCE_FILES
## But I guess???

build: $(EXECUTABLE)

## Deletes the executable (assumes small compile time)
clean:
    @rm -r -f $(shell find $(SRC)/ -type f -name '*.o')
    @rm $(EXECUTABLE)

.PHONY: build clean

$(EXECUTABLE): $(OBJECT_FILES)
    @$(CC) $(LDFLAGS) -o $@ $^
    @echo "Build successful!"

$(OBJECTS_FILES): %.o : $(SRC)/%.cpp
    @$(CC) $(CFLAGS) -o -c $@ $^

Well first you have a typo; 好吧,首先您有错字; you create a variable OBJECT_FILES : 您创建一个变量OBJECT_FILES

OBJECT_FILES := $(SOURCE_FILES: $(SRC)/%.cpp=%.o)

Also you shouldn't use spaces here (after the : ). 另外,您不应该在这里使用空格(在: )。 Then you use a variable named OBJECTS_FILES instead: 然后,您使用名为OBJECTS_FILES的变量代替:

$(OBJECTS_FILES): %.o : $(SRC)/%.cpp

That variable is empty so this is essentially a no-op. 该变量为空,因此本质上是空操作。

Your compiler invocation is wrong. 您的编译器调用是错误的。 The word after the -o option is the file the output should go to; -o选项后的单词是输出应转到的文件; yours is: 你的是:

@$(CC) $(CFLAGS) -o -c $@ $^

which means that the output of this command is going to the file -c . 这意味着该命令的输出将进入文件-c Then the input is an object file that doesn't exist... most likely you'll get an error. 然后输入是一个不存在的目标文件...很可能会出现错误。 Also you only want to pass the source file to the compiler, not all the other prerequisites (headers etc.) This should be: 另外,您只想将源文件传递给编译器,而不是所有其他先决条件(标头等),这应该是:

@$(CC) $(CFLAGS) -c -o $@ $<

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

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