简体   繁体   English

Makefile在Linux上运行但在Windows下无法运行,无法在子目录中找到文件

[英]Makefile working in Linux but not under Windows, can't find files in subdirectory

I have a makefile (gnu make) working under linux, but when porting it to windows it does not work. 我有一个在Linux下工作的makefile(gnu make),但是将其移植到Windows时不起作用。 The goal of the makefile is to make all the *.cpp file that reside in different subdirectories and compile them into *.obj files in a single BUILD_DIR. makefile的目标是制作所有位于不同子目录中的* .cpp文件,并将它们编译为单个BUILD_DIR中的* .obj文件。

Between linux and windows I only adjusted the SOURCES variable, the linux line is shown but commented. 在linux和Windows之间,我仅调整了SOURCES变量,显示了linux行,但已注释。 When I check all the names and directories they show the same (relative paths in their perspective notation) and what I expect. 当我检查所有名称和目录时,它们显示相同的内容(它们在透视符号中的相对路径)和我期望的内容。

The "error message" I get on windows is: make: *** No rule to make target 'DEM.cpp', needed by 'DEM.obj'. 我在Windows上收到的“错误消息”是:make:***没有规则来创建目标“ DEM.cpp”,这是“ DEM.obj”所需的。 Stop. 停止。

In debug mode it says: File 'DEM.cpp' does not exist. 在调试模式下,它说:文件'DEM.cpp'不存在。 (which it obviously does) (显然是这样)

On linux it finds the file via the VPATH, in debug mode it says: No need to remake target 'DEM.cpp'; 在linux上,它通过VPATH查找文件,在调试模式下,它说:无需重新制作目标文件'DEM.cpp'; using VPATH name './Code/DEM.cpp'. 使用VPATH名称“ ./Code/DEM.cpp”。

The subdirectory structure in both linux and windows is the same and the makefile is run from the same location. linux和Windows中的子目录结构相同,并且makefile从相同的位置运行。

Question: What is going wrong here, I have the feeling it has something to do with VPATH being handled differently on windows but I am not certain. 问题:这里出了什么问题,我感觉这与在Windows上以不同方式处理VPATH有关,但我不确定。

My makefile is: 我的makefile是:

# MAKEFILE    
# Some makefile settings 
SHELL = /bin/sh     #Make sure the shell is the standard shell
.SUFFIXES:      #Clear al implicit suffixes
.SUFFIXES: .cpp .obj    #Set used suffixes

# Variable Declaration
CXX := g++
BUILD_DIR = .\bin\Release
PROGRAM_NAME := DEM.exe

#SOURCES := $(shell find . -name '*.cpp')                   #All *.cpp files with directory (LINUX style)
SOURCES := $(shell FORFILES /S /M *.cpp /C "CMD /C ECHO @relpath")  #All *.cpp files with directory
NAMES := $(notdir $(basename $(SOURCES)))                   #Get all files names of the *.cpp files without extensions
OBJECTS := $(addsuffix .obj, $(NAMES))                      #Get the to be generate *.o files without directory
SRC_DIRS := $(dir $(SOURCES))                               #All the directory in which the sources are found
VPATH := $(SRC_DIRS) $(BUILD_DIR)

.PHONY: all
all: build                                  #Standard entry point, run release
    @echo "BUILD DONE"

.PHONY: build
    build: $(PROGRAM_NAME)
    @echo "BUILD DONE"

$(PROGRAM_NAME): $(OBJECTS)
    @echo "Link executable"
    $(CXX) -o $(BUILD_DIR)/$(PROGRAM_NAME) $(addprefix $(BUILD_DIR)/,$(OBJECTS))

$(OBJECTS): %.obj: %.cpp
    @echo "Compile into object code: $<"
    ${CXX} $(CXXFLAGS)  -c $< -o $(BUILD_DIR)/$@    #Compiles object code and places it in the $(BUILD_DIR)

Update 1: Based on the comment of some programmer dude , I ran it with -p, and got the following interesting result: 更新1:基于一些程序员dude的评论,我用-p运行它,并得到以下有趣的结果:

# Not a target:
DEM.cpp:
#  Implicit rule search has been done.
#  File does not exist.
#  File has not been updated.

# Not a target:
World.cpp:
#  Implicit rule search has not been done.
#  Modification time never checked.
#  File has not been updated.

# Lot more targets below

It seems only to not find DEM.cpp but it finds everything else. 似乎只是找不到DEM.cpp,而是找到其他所有内容。 DEM.cpp resides in C:\\Users\\dklomp\\Documents\\Programming\\C++ Source\\DEM\\DEM\\Code, but should resolve to .\\Code\\ Most other files reside in subdirectories in Code. DEM.cpp位于C:\\ Users \\ dklomp \\ Documents \\ Programming \\ C ++ Source \\ DEM \\ DEM \\ Code,但应解析为。\\ Code \\其他大多数文件位于Code的子目录中。 But stdafx.cpp also resides in .\\Code\\ and there is no problem finding it. 但是stdafx.cpp也驻留在。\\ Code \\中,并且找到它没有问题。

Can it be a name clash with the directory name DEM being similar to DEM.cpp 目录名称DEM是否类似于DEM.cpp可能是名称冲突?

Update 2: For information and closure. 更新2:有关信息和结束。 I had already check the VPATH by reading the variable by printing it this seemed to give the correct information. 我已经通过打印变量读取变量来检查VPATH,这似乎提供了正确的信息。 However if i read it with @(info $(VPATH)) it seemet empty: 但是,如果我用@(info $ {VPATH))阅读它,它似乎是空的:

@echo "VPATH print variable: "
@echo "$(VPATH)"
@echo "VPATH print info: "
$(info VPATH)

gave: 放弃:

"VPATH print variable: "
"./Code/InputOutput/ ./Code/InputOutput/ ./Code/InputOutput/ 
./Code/InputOutput/ ./Code/ ./Code/ ./Code/Models/ ./Code/Models/ 
./Code/Forces/ ./Code/Forces/ ./Code/Forces/ ./Code/Forces/ ./Code/Forces/ 
./Code/Forces/ ./Code/Forces/ ./Code/Forces/ ./Code/Forces/ ./Code/Tools/ 
./Code/Tools/ ./Code/Tools/ ./Code/Tools/ ./Code/Solvers/ ./Code/Solvers/ 
./Code/Solvers/ ./Code/World/ ./Code/World/ ./Code/World/ ./Code/World/ 
./Code/World/ ./Code/Interactions/ ./Code/Interactions/ ./Code/Interactions/ 
./Code/Interactions/ ./Code/Interactions/ ./Code/Interactions/                                                               
bin\Release"
"VPATH print info: "

Indeed with a lot of repetitions (will use the sort suggestion of hardcoreHenry) but it seemed oke. 确实有很多重复(将使用hardcoreHenry的排序建议),但是看起来不错。 However the VPATH info is empty for some reason. 但是,由于某些原因,VPATH信息为空。

However implementing the proposed solution by code_fodder and removing all the inline code comments works. 但是,通过code_fodder实现建议的解决方案并删除所有内联代码注释是可行的。 The inline code comments on the variable declaration do not seem to matter, those in the rules section do for windows, linux seems to handle inline code comments fine everywhere. 变量声明中的内联代码注释似乎无关紧要,rules部分中的注释对于Windows而言确实有用,Linux似乎可以在任何地方很好地处理内联代码注释。

As usually thanks for the help and suggestions. 像往常一样感谢您的帮助和建议。

I tested this in a path that had spaces before the current location (eg like d:\\path with spaces\\myfolder\\sub_folder1\\sub_folder2\\ ). 我在当前位置之前有空格的路径中对此进行了测试(例如, d:\\path with spaces\\myfolder\\sub_folder1\\sub_folder2\\ )。

I think there is some issue with the FORFILES syntax/output - at least I could not get it to work. 我认为FORFILES语法/输出存在一些问题-至少我无法使其正常工作。 As soon as I used the more generic rwildcard from second answer from here it started to work quite well. 从这里第二个答案开始使用更通用的rwildcard时,它就开始很好地工作。 Then I added rules to create/clean your output folder. 然后,我添加了创建/清理输出文件夹的规则。

Also I removed any inline comments, since I am not 100% sure they are valid because they leave space characters everywhere and makefile is not very tolerant of that... IE make-sure you don't leave any trailing white space on any lines. 我也删除了任何内联注释,因为我不是100%确信它们是有效的,因为它们到处都留有空格字符,并且makefile对此不太宽容... IE确保您不会在任何行上留下任何尾随空白。

Also - this all assumes its gnu-make and not nmake 另外-这一切都假设其是gnu-make而并非nmake

# MAKEFILE    
# Some makefile settings 
SHELL = /bin/sh
.SUFFIXES:
.SUFFIXES: .cpp .obj

# Variable Declaration
CXX := g++
BUILD_DIR = .\bin\Release
PROGRAM_NAME := DEM.exe

# Define a recursive wild card function that is portable.
rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))

#SOURCES := $(shell FORFILES /S /M *.cpp /C "CMD /C ECHO @relpath")
SOURCES := $(call rwildcard, ./, *.cpp)
$(info SOURCES: $(SOURCES))
NAMES := $(notdir $(basename $(SOURCES)))
OBJECTS := $(addsuffix .obj, $(NAMES))
SRC_DIRS := $(dir $(SOURCES))
VPATH := $(SRC_DIRS) $(BUILD_DIR)

.PHONY: all
all: create_dirs build
    @echo "BUILD DONE"

# Has an order dependency on create_dirs to ensure that happens first, and so 
# it works with parallel build (e.g. `make -j4`)
.PHONY: build
build: $(PROGRAM_NAME) | create_dirs
    @echo "BUILD DONE"

$(PROGRAM_NAME): $(OBJECTS)
    @echo "Link executable"
    $(CXX) -o $(BUILD_DIR)/$(PROGRAM_NAME) $(addprefix $(BUILD_DIR)/,$(OBJECTS))

$(OBJECTS): %.obj: %.cpp
    @echo "Compile into object code: $<"
    ${CXX} $(CXXFLAGS)  -c $< -o $(BUILD_DIR)/$@

.PHONY: create_dirs
create_dirs:
    @echo "creating dirs"
    mkdir -p $(BUILD_DIR)

.PHONY: clean
clean:
    @echo "cleaning"
    rm -rf $(BUILD_DIR)

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

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