MinGW在Windows上制作
MinGW make on Windows
我在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的情况下修复最初报告的问题,请执行以下
- 转到系统上安装MinGW的文件夹
- 搜索find.exe
- 将find.exe所在的文件夹添加到系统或用户环境变量中
- 不要忘记重新启动或关闭应用程序以使更改生效
- 您可以手动测试find命令,并可以看到它的工作原理
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 无法在windows上使用mingw将sqlite3与c连接
- 如何在 Linux for Windows 上通过 MinGW 构建静态 ICU 的 Boost
- 使用 MinGW 在 Windows 上安装 GMP 时出错
- C++:如何在Windows 10上使用编译器MinGW 9.2.0安装OpenCV
- 尝试在Windows上执行C++时出错(通过MinGW编译)
- 分段 Linux Ubuntu 中的 g++ 错误,但在 Windows 中的 g++/MingW 中,在 C++ 中打
- 使用 MinGW 在 Windows 上构建 GLEW 时遇到问题
- Windows cannot find MinGW
- 使用 Windows 10 和 MINGW 编译器在 VS Code 上使用 gdb 调试器进行调试时出错
- 使用 MinGW 在 Windows 上的命令行编译C++文件
- C++17 文件系统在 Windows 10 上使用 nuwen MinGW
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- Boost::process on Windows - with MinGW?
- CMake & MinGW Compileilation on Windows,无需 -G "MinGW Makefiles"标志
- CodeBlocks,C++,mingw-w64编译器,Windows 10,错误:对`DAQmxCreateTask&
- Python:在Windows上选择C++编译器 - MS Visual Studio到Mingw-w64.(PySta
- cppUnit with Windows+minGW
- 在c++中嵌入python代码(Windows + minGW + python 2.7.2 + Eclipse)
- c++, ussleep()已经过时,Windows/MingW的变通方法