如何配置才能使用cl命令编译C/C++程序?

晨曦之光 发布于 2012/03/09 14:14
阅读 2K+
收藏 0

本博客(http://blog.csdn.net/livelylittlefish )贴出作 者(三二一@小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正!

如何配置才能使用 cl 命令编译 C/C++ 程序?

 

VS 环境中,我们可以顺利的编译、生成程序。究其原因,是因为 VS IDE 本身已经配置好了编译程序所需的 executable file include files library 的路径。以 VS2005 为例,在其 IDE 环境中,通过 tools->options->Projects and Solutions->VC++ Directories ,即可查看已经配置好的 Executable files Include files Library files

 

我们知道, VS 也提供了命令行,那么,该如何配置,才能通过命令行编译、生成程序?

本文以 VS2005 为例,操作系统为 windows XP ,且笔者的机器中安装了 ACE Boost cppunit 等软件或库。 VS 及这些软件或库的安装目录如下。

 

D:/Program Files/Microsoft Visual Studio 8

E:/opensource/ACE-5.6.4/ACE_wrappers

E:/opensource/boost_1_40_0

E:/opensource/cppunit-1.12.1

 

1. 方法 1 :手动配置

 

下文的操作都是通过直接通过“系统 -> 属性 -> 高级 -> 环境变量 ”来添加或修改的,以便于立即生效。

 

(1) 添加公共变量

 

VSInstallDir=D:/Program Files/Microsoft Visual Studio 8

VCInstallDir=D:/Program Files/Microsoft Visual Studio 8/VC

FrameworkDir=C:/WINDOWS/Microsoft.NET/Framework

FrameworkVersion=v2.0.50727

 

(2) 修改 path 变量

 

在用户 path 变量后面添加如下路径:

 

%VCInstallDir%/BIN;

%VCInstallDir%/PlatformSDK/bin;

%VSInstallDir%/Common7/IDE;

%VSInstallDir%/Common7/Tools;

%VSInstallDir%/Common7/Tools/bin;

%VSInstallDir%/SDK/v2.0/bin;

%FrameworkDir%/%FrameworkVersion%;

%VCInstallDir%/VCPackages;

 

(3) 添加 INCLUDE 变量

 

%VCInstallDir%/ATLMFC/INCLUDE;

%VCInstallDir%/INCLUDE;

%VCInstallDir%/PlatformSDK/include;

%VSInstallDir%/SDK/v2.0/include;

%ACE_ROOT%;

%BOOST_1_40_ROOT%;

%CPPUNIT_ROOT%/include ;

 

(4) 添加 LIB 变量

 

%VCInstallDir%/ATLMFC/LIB;

%VCInstallDir%/LIB;

%VCInstallDir%/PlatformSDK/lib;

%VSInstallDir%/SDK/v2.0/lib;

%ACE_ROOT%/lib;

%BOOST_1_40_ROOT%/stage/lib;

%CPPUNIT_ROOT%/lib ;

 

其中,

ACE_ROOT=E:/opensource/ACE-5.6.4/ACE_wrappers

BOOST_1_40_ROOT=E:/opensource/boost_1_40_0

CPPUNIT_ROOT=E:/opensource/cppunit-1.12.1

当然,这几个变量是已经配置好的。

 

2. 方法 2 :自动配置

 

如果想省事,我们可以修改并执行以下 bat 文件,该文件会完成 VS2005 的环境变量的设置。

D:/Program Files/Microsoft Visual Studio 8/Common7/Tools /vsvars32.bat

 

文件修改前的内容如下,


@SET VSINSTALLDIR =D:/Program Files/Microsoft Visual Studio 8

@SET VCINSTALLDIR =D:/Program Files/Microsoft Visual Studio 8/VC

@SET FrameworkDir =C:/WINDOWS/Microsoft.NET/Framework

@SET FrameworkVersion =v2.0.50727

@SET FrameworkSDKDir =D:/Program Files/Microsoft Visual Studio 8/SDK/v2.0

 

@if "%VSINSTALLDIR%"=="" goto error_no_VSINSTALLDIR

@if "%VCINSTALLDIR%"=="" goto error_no_VCINSTALLDIR

 

@echo Setting environment for using Microsoft Visual Studio 2005 x86 tools.

 

@rem

@rem Root of Visual Studio IDE installed files.

@rem

@set DevEnvDir=D:/Program Files/Microsoft Visual Studio 8/Common7/IDE

 

@set PATH =D:/Program Files/Microsoft Visual Studio 8/Common7/IDE;D:/Program Files/Microsoft Visual Studio 8/VC/BIN;D:/Program Files/Microsoft Visual Studio 8/Common7/Tools;D:/Program Files/Microsoft Visual Studio 8/Common7/Tools/bin;D:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/bin;D:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/bin;C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727;D:/Program Files/Microsoft Visual Studio 8/VC/VCPackages;%PATH%

@set INCLUDE =D:/Program Files/Microsoft Visual Studio 8/VC/ATLMFC/INCLUDE;D:/Program Files/Microsoft Visual Studio 8/VC/INCLUDE;D:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/include;D:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/include;%INCLUDE%

@set LIB =D:/Program Files/Microsoft Visual Studio 8/VC/ATLMFC/LIB;D:/Program Files/Microsoft Visual Studio 8/VC/LIB;D:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/lib;D:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/lib;%LIB%

@set LIBPATH =C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727;D:/Program Files/Microsoft Visual Studio 8/VC/ATLMFC/LIB

 

@goto end

 

:error_no_VSINSTALLDIR

@echo ERROR: VSINSTALLDIR variable is not set.

@goto end

 

:error_no_VCINSTALLDIR

@echo ERROR: VCINSTALLDIR variable is not set.

@goto end

 

:end


如果不修改该文件,运行完 vsvars32.bat 文件,只能通过命令行编译、生成一般的 C/C++ 程序;所以,如果程序是基于诸如 ACE Boost 等库的程序,则编译、生成仍然会失败,因为 vsvars32.bat 文件只是设置了一些一般的 C/C++ 程序所需要的头文件和库目录。因此,为了能够编译 ACE Boost CppUnit 的程序,需要修改 vsvar32.bat 文件,修改如下

 


@SET VSINSTALLDIR =D:/Program Files/Microsoft Visual Studio 8

 

@SET VCINSTALLDIR =D:/Program Files/Microsoft Visual Studio 8/VC

@SET FrameworkDir =C:/WINDOWS/Microsoft.NET/Framework

@SET FrameworkVersion =v2.0.50727

@SET FrameworkSDKDir =D:/Program Files/Microsoft Visual Studio 8/SDK/v2.0

@SET ACE_ROOT=E:/opensource/ACE-5.6.4/ACE_wrappers

@SET BOOST_1_40_ROOT=E:/opensource/boost_1_40_0

@SET CPPUNIT_ROOT=E:/opensource/cppunit-1.12.1

 

@if "%VSINSTALLDIR%"=="" goto error_no_VSINSTALLDIR

@if "%VCINSTALLDIR%"=="" goto error_no_VCINSTALLDIR

 

@echo Setting environment for using Microsoft Visual Studio 2005 x86 tools.

 

@rem

@rem Root of Visual Studio IDE installed files.

@rem

@set DevEnvDir=D:/Program Files/Microsoft Visual Studio 8/Common7/IDE

 

@set PATH =D:/Program Files/Microsoft Visual Studio 8/Common7/IDE;D:/Program Files/Microsoft Visual Studio 8/VC/BIN;D:/Program Files/Microsoft Visual Studio 8/Common7/Tools;D:/Program Files/Microsoft Visual Studio 8/Common7/Tools/bin;D:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/bin;D:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/bin;C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727;D:/Program Files/Microsoft Visual Studio 8/VC/VCPackages;%PATH%

@set INCLUDE =D:/Program Files/Microsoft Visual Studio 8/VC/ATLMFC/INCLUDE;D:/Program Files/Microsoft Visual Studio 8/VC/INCLUDE;D:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/include;D:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/include; %ACE_ROOT%;%BOOST_1_40_ROOT%;%CPPUNIT_ROOT%/include ; %INCLUDE%

@set LIB =D:/Program Files/Microsoft Visual Studio 8/VC/ATLMFC/LIB;D:/Program Files/Microsoft Visual Studio 8/VC/LIB;D:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/lib;D:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/lib; %ACE_ROOT%/lib;%BOOST_1_40_ROOT%/stage/lib;%CPPUNIT_ROOT%/lib ; %LIB%

@set LIBPATH =C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727;D:/Program Files/Microsoft Visual Studio 8/VC/ATLMFC/LIB

 

@goto end

 

:error_no_VSINSTALLDIR

@echo ERROR: VSINSTALLDIR variable is not set.

@goto end

 

:error_no_VCINSTALLDIR

@echo ERROR: VCINSTALLDIR variable is not set.

@goto end

 

:end


红色部分即为添加的内容。

 

 

 

Appendix 笔者机器上的环境变量

 

C:/Documents and Settings/zubo.yu/Desktop>echo %PATH%

C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem;D:/Program Files/Cppcheck/;C:/Program Files/

Rational/ClearCase/bin;C:/Program Files/Common Files/Thunder Network/KanKan/Codecs;C:/Program Files/

Rational/common;D:/Program Files/TortoiseSVN/bin;D:/Program Files/UltraEdit/;E:/opensource/ACE-5.6.4

/ACE_wrappers/lib;D:/Program Files/Microsoft Visual Studio 8/VC/BIN;D:/Program Files/Microsoft Visua

l Studio 8/VC/PlatformSDK/bin;D:/Program Files/Microsoft Visual Studio 8/Common7/IDE;D:/Program File

s/Microsoft Visual Studio 8/Common7/Tools;D:/Program Files/Microsoft Visual Studio 8/Common7/Tools/b

in;D:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/bin;C:/WINDOWS/Microsoft.NET/Framework/v2.0.5

0727;D:/Program Files/Microsoft Visual Studio 8/VC/VCPackages;

 

 

C:/Documents and Settings/zubo.yu/Desktop>echo %INCLUDE%

D:/Program Files/Microsoft Visual Studio 8/VC/ATLMFC/INCLUDE;D:/Program Files/Microsoft Visual Studi

o 8/VC/INCLUDE;D:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/include;D:/Program Files/Mi

crosoft Visual Studio 8/SDK/v2.0/include;E:/opensource/ACE-5.6.4/ACE_wrappers;E:/opensource/boost_1_

40_0;E:/opensource/cppunit-1.12.1/include

 

 

C:/Documents and Settings/zubo.yu/Desktop>echo %LIB%

D:/Program Files/Microsoft Visual Studio 8/VC/ATLMFC/LIB;D:/Program Files/Microsoft Visual Studio 8/

VC/LIB;D:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/lib;D:/Program Files/Microsoft Visu

al Studio 8/SDK/v2.0/lib;E:/opensource/ACE-5.6.4/ACE_wrappers/lib;E:/opensource/boost_1_40_0/stage/l

ib;E:/opensource/cppunit-1.12.1/lib

 

画线部分为手动配置中新加入的目录。



原文链接:http://blog.csdn.net/livelylittlefish/article/details/6093903
加载中
返回顶部
顶部