如何创建一个.bat批处理文件,使用命令提示符自动编译C ++文件? [英] How to create a .bat batch file that automatically compiles a C++ file with command prompt?

查看:577
本文介绍了如何创建一个.bat批处理文件,使用命令提示符自动编译C ++文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建.bat文件时,我应该使用哪些代码行,该文件将使用命令提示符自动编译c ++源文件并自动运行.exe?假设我使用visual c ++构建工具并假设.bat文件具有到源的路径。 (在Windows上)



请举例说明Visual c ++构建工具编译器。



我尝试了什么:



批处理文件中的代码:

 cd c:\ < path-to-directory>  
cl / EHsc program.cpp
program.exe

解决方案

您必须首先为命令行构建设置路径和环境变量。请参阅在命令行上构建C / C ++代码| Microsoft Docs [ ^ ]。



如果您已创建VS项目,则可以使用 msbuild 命令行实用程序构建它。请参阅 MSBuild命令行参考 - Visual Studio | Microsoft Docs [ ^ ]。



未经测试的例子:

 SET VS = C:\Program Files(x86)\ Microsoft Visual Studio 14.0 
SET PROJ = path_to_your_project
CALL%VS%\ Common7 \ Tools \vcvars32.bat
cl .exe / EHsc%PROJ%\ program.cpp
IF NOT%ERRORLEVEL%== 0 GOTO完成
%PROJ%\ program.html
:完成


请参阅快速构建IDE [ ^ ]。

What lines of code should I use when creating a .bat file that will automatically compile a c++ source file using command prompt and auto run the .exe ? assuming I am using visual c++ build tools and assuming the .bat file has the paths to the source. (on Windows)

Please give example specific to Visual c++ build tools compiler.

What I have tried:

code inside the batch file:

cd c:\<path-to-directory>
cl /EHsc program.cpp
    program.exe

解决方案

You have to set the path and environment variables for command line builds first. See Build C/C++ Code on the Command Line | Microsoft Docs[^].

If you have created a VS project you can build it with the msbuild command line utility. See MSBuild Command-Line Reference - Visual Studio | Microsoft Docs[^].

Untested example:

SET VS=C:\Program Files (x86)\Microsoft Visual Studio 14.0
SET PROJ=path_to_your_project
CALL "%VS%\Common7\Tools\vcvars32.bat"
cl.exe /EHsc "%PROJ%\program.cpp"
IF NOT %ERRORLEVEL%==0 GOTO Done
"%PROJ%\program.exe"
:Done


See Quick Build Without the IDE[^].


这篇关于如何创建一个.bat批处理文件,使用命令提示符自动编译C ++文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆