MinGW在Windows上制作

MinGW make on Windows

本文关键字:Windows MinGW      更新时间:2023-10-16

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

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

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

这是我使用的makefile(我从这个博客中改编了它,以及博客中的基本项目结构:

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不能。这是有原因的吗?

Windows上的find命令与Unix上的find 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 $@ $<
# 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的另一种选择是简单地列出源文件。一般来说,这是一个比动态生成列表更好的主意。

要在不更改makefile的情况下修复最初报告的问题,请执行以下

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