在阅读本文前请先确认你已经了解了
http://blog.csdn.net/dcmilan/article/details/7983697
http://blog.csdn.net/dcmilan/article/details/7986961
1.安装MSYS
MSYS在mingw的官网上可以下载,我这里使用1.0版本
安装完之后在C:\msys\1.0\bin文件夹里会有GNU的make
在cmd下使用make时,需要显示调用全路径C:\msys\1.0\bin\make
2.准备cpp和makefile
// hello.cpp
#include <iostream>
int main( )
{
std::cout << "Hello, World!\n";
}
// makefile(这一行在代码中不要出现,makefile中的命令行部分要以制表符tab开头,而不是空格)
# This is the default target, which will be built when
# you invoke make
.PHONY: all
all: hello
# This rule tells make how to build hello from hello.cpp
hello: hello.cpp
g++ -o hello hello.cpp
# This rule tells make to copy hello to the binaries subdirectory,
# creating it if necessary
.PHONY: install
install:
C:/msys/1.0/bin/mkdir -p binaries
C:/msys/1.0/bin/cp hello binaries
# This rule tells make to delete hello and hello.o
.PHONY: clean
clean:
C:/msys/1.0/bin/rm -f hello
建立一个文件夹hello,然后将以上代码在hello文件夹里创建对应文件hello.cpp和makefile
3.执行make命令
在cmd环境下切换目录到hello下
执行命令C:/msys/1.0/bin/make hello
生成hello.exe
然后再执行C:/msys/1.0/bin/make install
创建一个binaries文件夹,并将hello.exe复制到其中
注意:这里的makefile是完整的文件名,make命令
会自动找到这个文件,并执行文件中对应段的操作