一直以來我只知道vc設置命令行編譯環境的批處理命令是%VS140COMNTOOLS%/Common7/Tools下的vsvars32.bat,(%VS140COMNTOOLS%為定義vs2015公共工具程序位置的環境變量,命令行窗口輸入set就能找到,vs2010的對應定義為%VS100COMNTOOLS%)
微軟的官網上有這個命令的說明:
https://technet.microsoft.com/zh-cn/library/1700bbwd.aspx
但在執行這個命令生成的命令行環境下用nmake只能編譯32位版本的代碼,我一直都不知道如何用nmake編譯64位的代碼。
今天才搞明白vsvars32.bat已經過時了,正確的打開方式是vcvarsall.bat 。
至少從vs2010開始,
%VS140COMNTOOLS%/VC下就有vcvarsall.bat,用於生成命令行編譯環境。
如果要在命令行生成 32位代碼,就執行vcvarsall x86
如果要在32位系統下生成64位代碼,就執行vcvarsall x86_amd64
如果要在64位系統下生成32位代碼,就執行vcvarsall x86或vcvarsall amd64_x86
到了VS2015,已經支持arm平台了,所以如果要生成arm平台的代碼,就執行vcvarsall x86_arm 如果你的操作系統是64位的也可以 vcvarsall amd64_arm
前面一個名字代表你的當前電腦的體系結構,后面的這個名字代表你要生成的代碼的體系結構。如果兩個名字一樣,就簡化為一個名字。
搞清楚了這個,今天終於順利在命令行下實現nmake編譯32位和64位版本代碼:
生成用於編譯32位代碼的命令行編譯環境
C:\Program Files (x86)\Microsoft Visual Studio 14.0>cd VC C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC>vcvarsall x86
執行cmake生成NMake格式的Makefile,指定處理器為x86,然后執行nmake編譯所有代碼,並安裝
>cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=RELEASE -DTARGET_PROCESSOR=x86 ..\facecl >nmake install
安裝成功的輸出
Install the project...
-- Install configuration: "RELEASE" -- Installing: C:/Program Files (x86)/facecl/lib/static/detect_cl.lib -- Installing: C:/Program Files (x86)/facecl/bin/detect_cl.dll -- Installing: C:/Program Files (x86)/facecl/lib/static/img_tool.lib -- Installing: C:/Program Files (x86)/facecl/bin/img_tool.dll -- Installing: C:/Program Files (x86)/facecl/bin/test_detect.exe -- Up-to-date: C:/Program Files (x86)/facecl/./README_utf8.txt -- Up-to-date: C:/Program Files (x86)/facecl/include/detect_cl_types.h -- Up-to-date: C:/Program Files (x86)/facecl/include/detect_cl.h -- Up-to-date: C:/Program Files (x86)/facecl/include/img_tool.h -- Up-to-date: C:/Program Files (x86)/facecl/sample/test_detect.cpp -- Up-to-date: C:/Program Files (x86)/facecl/sample/utility.h -- Up-to-date: C:/Program Files (x86)/facecl/sample/raii.h -- Up-to-date: C:/Program Files (x86)/facecl/sample/assert_macros.h -- Up-to-date: C:/Program Files (x86)/facecl/sample/cmdline.h -- Up-to-date: C:/Program Files (x86)/facecl/sample/dirent.h -- Up-to-date: C:/Program Files (x86)/facecl/sample/file_utilits.h -- Up-to-date: C:/Program Files (x86)/facecl/sample/time_utilits.h
如果要在32位系統下生成64位代碼,也如法炮制
關於vcvarsall.bat更詳細的說明,參見微軟的官方文檔:
https://msdn.microsoft.com/zh-cn/library/f2ccy3wt.aspx
