<i id='2CEei'><tr id='2CEei'><dt id='2CEei'><q id='2CEei'><span id='2CEei'><b id='2CEei'><form id='2CEei'><ins id='2CEei'></ins><ul id='2CEei'></ul><sub id='2CEei'></sub></form><legend id='2CEei'></legend><bdo id='2CEei'><pre id='2CEei'><center id='2CEei'></center></pre></bdo></b><th id='2CEei'></th></span></q></dt></tr></i><div id='2CEei'><tfoot id='2CEei'></tfoot><dl id='2CEei'><fieldset id='2CEei'></fieldset></dl></div>

    <bdo id='2CEei'></bdo><ul id='2CEei'></ul>

    <small id='2CEei'></small><noframes id='2CEei'>

      <legend id='2CEei'><style id='2CEei'><dir id='2CEei'><q id='2CEei'></q></dir></style></legend>
    1. <tfoot id='2CEei'></tfoot>
    2. 在 Visual Studio 中运行小型 C++ 程序而无需创建项目

      Running small C++ programs in Visual Studio without creating projects(在 Visual Studio 中运行小型 C++ 程序而无需创建项目)
        <bdo id='jtUAw'></bdo><ul id='jtUAw'></ul>

        <legend id='jtUAw'><style id='jtUAw'><dir id='jtUAw'><q id='jtUAw'></q></dir></style></legend>

            <tfoot id='jtUAw'></tfoot>

            <i id='jtUAw'><tr id='jtUAw'><dt id='jtUAw'><q id='jtUAw'><span id='jtUAw'><b id='jtUAw'><form id='jtUAw'><ins id='jtUAw'></ins><ul id='jtUAw'></ul><sub id='jtUAw'></sub></form><legend id='jtUAw'></legend><bdo id='jtUAw'><pre id='jtUAw'><center id='jtUAw'></center></pre></bdo></b><th id='jtUAw'></th></span></q></dt></tr></i><div id='jtUAw'><tfoot id='jtUAw'></tfoot><dl id='jtUAw'><fieldset id='jtUAw'></fieldset></dl></div>
              1. <small id='jtUAw'></small><noframes id='jtUAw'>

                  <tbody id='jtUAw'></tbody>
                本文介绍了在 Visual Studio 中运行小型 C++ 程序而无需创建项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                有没有什么方法可以在不创建项目的情况下在 Visual Studio 中构建/运行小型 C++ 程序?

                Is there any way to build/run small C++ programs, in Visual Studio without creating projects?

                例如,如果我有一个文件hello.cpp,我可以在没有项目的情况下将其编译为hello.exe吗?

                For example, if I have a file hello.cpp, can I compile it to hello.exe without a project?

                推荐答案

                GMan 的想法 拥有一个沙箱"项目是一个很好的想法,因为它允许轻松尝试多文件测试.我称我的为cppTest".

                GMan's idea of having a 'sandbox' project is a good one, as it allows for easily trying out multi-file tests. I call mine "cppTest".

                但是,如果您只想编译您碰巧打开的任何 C 或 C++ 文件,只需创建一个简单的外部工具"即可.实际上,事情并没有想象中的那么简单.

                However, if you just want to be able to compile whatever C or C++ file you happen to have open, just create a simple "External Tool". Actually, it's not as simple as it probably should be.

                首先,创建一个批处理文件,用于设置编译器环境并运行编译器.类似于以下内容:

                First, create a batch file that will set up the compiler environment and run the compiler. Something like the following:

                @rem - runcl.cmd
                @rem     a batch file to drive simple VC9 compiles
                #rem
                @echo off
                
                set LIBRARIES=kernel32.lib user32.lib advapi32.lib shlwapi.lib oleaut32.lib
                set WIN32_WINNT=0x0500
                
                call "%ProgramFiles%Microsoft Visual Studio 9.0Common7Toolsvsvars32.bat"
                
                echo Visual C/C++ 2008 (VC 9.0) Compile...
                set CC="%ProgramFiles%Microsoft Visual Studio 9.0VCincl.exe" /Zi /EHsc -D_WIN32_WINNT=%WIN32_WINNT% %1 /link /incremental:no %LIBRARIES%
                echo %CC%
                %CC%
                

                在工具/外部工具..."对话框中,添加一个新项目并填写以下字段:

                In the "Tools/External Tools..." dialog, add a new item and fill in the following fields:

                • 标题:&编译文件
                • 命令:c:path o uncl.cmd
                • 参数:$(ItemPath)
                • 初始目录:$(ItemDir)

                选中使用输出窗口"框

                现在您可以使用工具"菜单中的编译文件"项来编译当前打开的文件.您甚至可以双击输出窗口中的错误,将您带到有错误的行.

                Now you can use the "Compile File" item in the Tools menu to compile whatever is the currently open file. You can even double click on the errors in the output window to take you to the lines with errors.

                这有一些限制,您可以通过创建批处理文件或使用 Visual Studio 宏来修复其中一些限制(我对 VS 宏不是很熟悉).

                There are some limitations with this some of which you can fix by fancying up the batch file or maybe with a Visual Studio macro (I'm not very familiar with VS macros).

                1. 如果您还没有保存文件,编译将针对最近保存的文件运行.外部工具配置中没有强制保存的选项.
                2. 如果您运行该命令并且没有处于活动状态的 C 或 C++ 文件,则批处理文件将失败
                3. 批处理文件可能还有很多其他地方会混淆

                像这样的批处理文件的好处在于,您还可以使用它来帮助将编译器集成到各种编辑器中,以便您调用外部工具,例如 UltraEdit、Zeus Editor、EditPad Pro、PSPad、Programmer's Notepad 等.等

                The nice thing about a batch file like this is that you can also use it to help integrate the compiler into various editors that let you call external tools, like UltraEdit, Zeus Editor, EditPad Pro, PSPad, Programmer's Notepad, etc. etc.

                如果您愿意,我已经发布了一个批处理文件,我用来将多个编译器集成到上述编辑器中.该批处理文件处理多个编译器,包括从第 6 版到第 9 版的各种 MSVC 编译器、Digital Mars、MinGW 和 Comeau 编译器(要使用的编译器由附加参数选择).批处理文件非常复杂(不幸的是,这是具有任何复杂性的 Windows 批处理文件的性质).但我发现它使得从各种编辑器运行这些东西变得非常简单.我可以快速按下分配给编译器的几个键来针对 5 个不同的编译器编译单个文件,这样我就可以轻松测试兼容性.

                If you like, I've posted a batch file that I use to integrate several compilers into editors like the above. This batch file handles several compilers including various MSVC compilers from version 6 through version 9, Digital Mars, MinGW and Comeau compilers (the compiler to use is selected by an additional parameter). The batch file is pretty convoluted (unfortunately, that's the nature of Windows batch files that have any kind of complexity). But I find it makes running these things from various editors pretty simple. I can quickly hit a few keys that I've assigned to the compilers to compile a single file against 5 different compilers so I can test for compatibility easily.

                除了我觉得它对我自己的目的有用之外,我不做任何承诺 - 如果你也这样做,那就太好了.否则,不要使用它...

                I make no promises about it other than I find it useful for my own purposes - if you do too, great. Otherwise, don't use it...

                • https://gist.github.com/mburr/3308168

                这篇关于在 Visual Studio 中运行小型 C++ 程序而无需创建项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                Delay Loading DLLs(延迟加载 DLL)
                Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                Loading a dll from a dll?(从 dll 加载 dll?)
              2. <i id='TWGQX'><tr id='TWGQX'><dt id='TWGQX'><q id='TWGQX'><span id='TWGQX'><b id='TWGQX'><form id='TWGQX'><ins id='TWGQX'></ins><ul id='TWGQX'></ul><sub id='TWGQX'></sub></form><legend id='TWGQX'></legend><bdo id='TWGQX'><pre id='TWGQX'><center id='TWGQX'></center></pre></bdo></b><th id='TWGQX'></th></span></q></dt></tr></i><div id='TWGQX'><tfoot id='TWGQX'></tfoot><dl id='TWGQX'><fieldset id='TWGQX'></fieldset></dl></div>
                  <bdo id='TWGQX'></bdo><ul id='TWGQX'></ul>

                  <small id='TWGQX'></small><noframes id='TWGQX'>

                    <tbody id='TWGQX'></tbody>

                    1. <tfoot id='TWGQX'></tfoot>
                      <legend id='TWGQX'><style id='TWGQX'><dir id='TWGQX'><q id='TWGQX'></q></dir></style></legend>