MinGW在Windows上制作

时间:2014-09-29 23:12:07

标签: c++ windows makefile mingw

我在我的Windows 8.1机器上安装了MinGW,我正在使用它来开发可以移植到Unix / Linux系统的C ++代码,而不是能够双启动Ubuntu或类似的。

我有一个应该用于构建项目的通用makefile,但由于某种原因,Windows上的find命令无法找到项目的源文件。我收到这个错误:

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

这是我使用的makefile(我从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 $@ $<"; $(CC) $(CFLAGS) $(INC) -c -o $@ $<

# 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则不能。有这个原因吗?

2 个答案:

答案 0 :(得分:2)

Windows上的find命令与Unix上的find find命令完全不同。它在Unix上执行的功能更像grep。你的makefile通常假设有许多Unix实用程序,因此不能在Windows上运行,或者至少不会,除非你使用提供它所期望的所有Unix命令的东西。你可以使用Cygwin或MSYS,后者是前者的精简版。 Cygwin默认创建Cygwin应用程序,而MSYS默认创建&#34; native&#34; 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 $@ $<

# 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的另一种方法是简单地列出源文件。通常,这比动态生成列表更好。

答案 1 :(得分:1)

要在不更改makefile的情况下解决最初报告的问题,请执行以下操作

  1. 转到系统上安装了MinGW的文件夹
  2. 搜索find.exe
  3. 将find.exe所在的文件夹添加到系统或用户环境vars。
  4. 不要忘记重新启动或关闭应用程序以使更改生效。
  5. 您可以手动测试find命令,并且可以看到