在我当前的示例中,我有一个名为“Main.c”的 C 文件,该文件将美制磅转换并打印为英石/英制磅/千克,变量美制磅当前由用户输入 (scanf) 定义。文件本身通过 Visual Express 2013 执行;但是,我想更改此设置,以便我可以导航到 C 文件并直接通过命令提示符 (cmd) 执行该文件,同时传递一个值来定义美制磅。
我知道这在 main 中是必需的:
void main(int argc, char *argv[])
而且我知道如何通过命令提示符导航到 C 文件:
>cd Directory/To/The/File
然而,这就是我卡住的地方;我不知道如何执行C文件。我对此进行了研究并找到了几个示例,例如使用“gcc”和“cc”,但系统无法识别这些命令。我找到的所有 Material 都没有完全解释我必须做什么才能完成我想做的事情;我是否必须安装某些东西,或者我是否使用了错误的命令,我可能正在尝试做什么,我究竟需要做什么?
最佳答案
一般来说,您需要执行以下操作:
- 编译 使用编译器 生成可执行文件 的程序。
- 运行可执行文件。
当您“在 Visual Studio 中运行程序”时,它会为您执行所有这些步骤。
我不太了解 Windows 中的命令行编译,但我相信 msbuild
是 Visual Studio 使用的编译器的名称。研究一下,您将了解如何编译。然后编译将生成可执行文件,您只需在命令行中输入即可运行。
编辑:我找到了 an article这表明 cl
是命令行上可用的 C 编译器命令。
这里是相关的摘录:
- At the command prompt, specify the cl command together with the name of your source file—for example, cl simple.c—and press Enter to compile the program. The cl.exe compiler generates an .obj file that contains the compiled code, and then runs the linker to build an executable program that has the name of your source file, but has an .exe file name extension—for example, Simple.exe.
关于c - 如何通过cmd执行C文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25997041/