2014-09-29 70 views
0

我的Windows 8.1機器上安裝了MinGW,我正在使用它來開發可移植到Unix/Linux系統的C++代碼,以代替能夠雙啓動Ubuntu或類似系統。MinGW在Windows上製作

我有一個通用的生成文件,應該用於構建項目,但由於某種原因,Windows上的find命令無法找到該項目的源文件。我得到這個錯誤:

File not found - *.cpp 
Makefile:32: *** mixed implicit and normal rules. Stop. 

下面是我用(我將它改編自this博客,從博客的基本工程結構一起生成文件:

CC := g++ # This is the main compiler 
# CC := clang --analyze # and comment out the linker last line for sanity 
SRCDIR := src   # Directory for source code 
BUILDDIR := build  # Directory containing all object files, which are removed on "make clean" 
TARGET := bin/runner # bin/runner contains the main executable for project 
        # bin/ contains all other executables in the project (such as tests) 

SRCEXT := cpp   # File extension of source code 

# Look for all the source files in SRCDIR with the file extension specified above 
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT)) 

# Name all object files the same root name as the source files from which they came, but add a .o extension to the end 
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o)) 

# The -g flag specifies that debugging information should be produced in the native format of the OS 
CFLAGS := -g # -Wall 

# Various flags for libraries that might need to be linked 
LIB := -pthread -lmongoclient -L lib -lboost_thread-mt -lboost_filesystem-mt -lboost_system-mt 

# Ensures that all header files (in the include/ folder) are accessible for build 
INC := -I include 

# Show the components that are currently being compiled/linked 
# Also, this is the main procedure for make: The TARGET is built from the objects, and 
# object files are built from source 
$(TARGET): $(OBJECTS) 
    @echo " Linking..." 
    @echo " $(CC) $^ -o $(TARGET) $(LIB)"; $(CC) $^ -o $(TARGET) $(LIB) 

$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT) 
    @mkdir -p $(BUILDDIR) 
    @echo " $(CC) $(CFLAGS) $(INC) -c -o [email protected] $<"; $(CC) $(CFLAGS) $(INC) -c -o [email protected] $< 

# Directives for "make clean" which cleans all object files out of the build/ folder 
clean: 
    @echo " Cleaning..."; 
    @echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET) 

# Tests 
# tester: 
    # $(CC) $(CFLAGS) test/tester.cpp $(INC) $(LIB) -o bin/tester 

# Spikes 
# ticket: 
    # $(CC) $(CFLAGS) spikes/ticket.cpp $(INC) $(LIB) -o bin/ticket 

# Destroys everything in the build/ and bin/runner/ folders. Does not clean test executables. 
.PHONY: clean 

如果我打開一個增強的控制檯我的項目的根目錄,並運行find src/ -name '*.cpp',我得到同樣的錯誤,如上所示。從根目錄運行dir Windows命令也找不到cpp文件。但是,從src目錄運行時,dir可以找到我的所有源文件,而find cann OT。是否有一個原因?

回答

1

Windows上的find命令與Unix上的查找find命令完全不同。它在Unix上執行更像grep的功能。你的makefile通常假定有許多Unix實用程序,所以它們在Windows上無法使用,或者至少不會使用它,除非你使用提供所有Unix命令的東西。你可以使用Cygwin或MSYS,後者是前者的精簡分支。 Cygwin默認創建Cygwin應用程序,而MSYS默認創建「原生」Windows應用程序。

也可以重寫makefile以使用Windows命令。喜歡的東西:

# Look for all the source files in SRCDIR with the file extension specified above 
SOURCES := $(shell dir /b /s $(SRCDIR)\*.$(SRCEXT)) 

... 

$(TARGET): $(OBJECTS) 
     @echo Linking... 
     $(CC) $^ -o $(TARGET) $(LIB) 

$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT) 
     -mkdir $(BUILDDIR) 
     $(CC) $(CFLAGS) $(INC) -c -o [email protected] $< 

# Directives for "make clean" which cleans all object files out of the build/ folder 
clean: 
     @echo Cleaning...; 
     -rmdir /s /q $(BUILDDIR) 
     -del $(TARGET) 

使用dir /b /s另一種方法是到只是簡單的列表的源文件。通常這比動態生成列表更好。