用NMAKE创建VS2012 C++工程 HelloWorld

由于需要精通GDAL的源代码,所以还是有必要精通NMAKE,先来尝试创建一个NMAKE工程。

之前一篇文章Windows7中Emacs 24 shell使用Gitbash已经介绍了如何在Emacs的shell中启动gitbash进程,这样就可以轻易的利用gitbash管理git,并且能够调用很多Linux命令。

本文参考:

http://bojan-komazec.blogspot.com/2011/10/how-to-use-nmake-and-makefile.html

还有我之前的一篇普通Windows控制台窗口运行nmake编译VC

先创建目录:

mkdir HelloWorld
cd HelloWorld


用Emacs创建man.cpp文件。

#include <iostream>

int main()
{
  std::cout << "Hello, world!" << std::endl;
  return 0;
}


再创建一个makefile文件。

foo: main.cpp
	cl main.cpp

这个makefile文件只包含了一个描述块(Description Block)

语法则是常见的makefile规则:

targets... : dependents...
    commands...


现在启动windows command窗口,最普通的那种。

然后运行设置环境变量的批处理文件,来自于普通Windows控制台窗口运行nmake编译VC

C:study
make>vc_env.bat x86
Setting environment for using Microsoft Visual Studio 2012 x86 tools.

C:study
make>where nmake
C:Program Files (x86)Microsoft Visual Studio 11.0VCin
make.exe

C:study
make>where cl.exe
C:Program Files (x86)Microsoft Visual Studio 11.0VCincl.exe

C:study
make>where link.exe
C:Program Files (x86)Microsoft Visual Studio 11.0VCinlink.exe


然后进入HelloWorld目录,运行nmake 命令:

C:study
makeHelloWorld>nmake

Microsoft (R) Program Maintenance Utility Version 11.00.60610.1
Copyright (C) Microsoft Corporation.  All rights reserved.

        cl main.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.60610.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

main.cpp
C:Program Files (x86)Microsoft Visual Studio 11.0VCINCLUDExlocale(336) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
Microsoft (R) Incremental Linker Version 11.00.60610.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:main.exe
main.obj

有一个警告,以后解决,不过已经看到cl命令了,cl main.cpp,并且最后生成了main.exe和main.obj文件。

现在运行main.exe程序,得到结果:

C:study
makeHelloWorld>main.exe
Hello, world!

VC编译包含两步骤,cl生成obj文件, link将obj文件连接成binary。这里由于没有使用/c参数,所以自动完成了link操作。



原文地址:https://www.cnblogs.com/riskyer/p/3285807.html