windows 我们如何在命令行开发环境中编写程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4391027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do we write a program in Command line development environment?
提问by fuddin
I have been writing my code in IDE,I just read that there also existed a Command Line Development Environment in which the code is written in DOS.I googled but found no results on how to use the command line development environment.My OS is Windows XP.I would be very thankful for your help me write the hello world program in DOS and also explain how to run it.
我一直在 IDE 中编写我的代码,我刚刚读到还有一个命令行开发环境,其中的代码是用 DOS 编写的。 XP。我将非常感谢您帮助我在 DOS 中编写 hello world 程序并解释如何运行它。
回答by Michael Burr
You simply use whatever text editor you like to create the C sourse file(s) then invoke the compiler command line(s) to compile and link the program (typically, an IDE is doing exactly that, but in a behind-the-scene manner). How the command line is invoked depends on the exact toolchain you're using.
您只需使用您喜欢的任何文本编辑器来创建 C 源文件,然后调用编译器命令行来编译和链接程序(通常,IDE 正是这样做的,但在幕后方式)。命令行的调用方式取决于您使用的确切工具链。
You might also need to set up an environment for you particular compiler toolchain (the right paths and various other env variables might need set up).
您可能还需要为特定的编译器工具链设置环境(可能需要设置正确的路径和各种其他环境变量)。
For Visual C++ the environment might be set up using a batch file installed by Visual Studio:
对于 Visual C++,可以使用 Visual Studio 安装的批处理文件设置环境:
vcvarsall x86
Invoking the compiler could be as simple as:
调用编译器可以很简单:
cl helloworld.c
or for C++ (for some reason it issues a non-fatal warning if you don't give it an option configuring details about how it should implement exceptions):
或者对于 C++(出于某种原因,如果您没有给它一个选项来配置它应该如何实现异常的详细信息,它会发出一个非致命警告):
cl /EHsc helloworld.cpp
The particulars are very dependent on the compiler you're using - you should read the docs for that compiler.
具体细节非常依赖于您使用的编译器 - 您应该阅读该编译器的文档。
Also, the options you use depend on your particular situation and needs. Scripts/batch files and/or makefile can help you manage the complexity of the options you might need to use.
此外,您使用的选项取决于您的特定情况和需求。脚本/批处理文件和/或 makefile 可以帮助您管理可能需要使用的选项的复杂性。
回答by Paul Tomasi
DOS is not dead.... yet!
DOS 还没有死……还没有!
fahad
法赫德
There are a number of methods by which you can enter code in DOS (see EDIT further on down).
您可以通过多种方法在 DOS 中输入代码(请参阅下面的 EDIT)。
(1) You can send keystrokes directly to a file
(1) 您可以将击键直接发送到文件
You do this by redirecting output to CON(the console) to a file. The only oddity of this method is that you end the 'session' by entering a CTRL-Zwhen you are finished.
您可以通过将输出重定向到CON(控制台)到一个文件来实现。这种方法的唯一奇怪之处在于,您在完成后通过输入CTRL-Z 来结束“会话” 。
It's basic, but this is how it goes.
这是基本的,但这就是它的方式。
Firstly, suppose you want to display "Hello World" on the screen, a simple batch file containing the following two lines is all that is required:
首先,假设您想在屏幕上显示“Hello World”,只需要一个包含以下两行的简单批处理文件:
@echo off
echo Hello World
The '@echo off' is commonly found at the start of all batch files. It simply instructs the command interpretter NOT to display each command as it is being executed (or parsed).
“ @echo off”通常出现在所有批处理文件的开头。它只是指示命令解释器不要在执行(或解析)每个命令时显示它。
One more thing before we start. Throughout this answer, I will assume your program is named 'helloworld.bat'.
在我们开始之前还有一件事。在整个答案中,我将假设您的程序名为“ helloworld.bat”。
Enter the following lines one after the other pressing the ENTER key at the end of each line:
在每行末尾按 ENTER 键,依次输入以下行:
copy con helloworld.bat
@echo off
echo Hello World
^Z
The '^Z' is displayed when you press the CTRL-Zkey combination (don't forget to press the ENTER key as well).
当您按下CTRL-Z组合键时会显示“ ^Z” (不要忘记同时按下 ENTER 键)。
When you press the ENTERkey after CTRL-Z, DOS displays the familiar '1 File(s) copied' messege.
当您在CTRL-Z之后按ENTER键时,DOS 会显示熟悉的“已复制 1 个文件”消息。
You can now execute the batch file program by simply entering the program's name like this:
您现在可以通过简单地输入程序名称来执行批处理文件程序,如下所示:
helloworld
And DOS will display the following:
并且 DOS 将显示以下内容:
Hello World
It can't get any more basic than that.
没有比这更基本的了。
(2) You can use DOS' EDIT program
(2) 可以使用DOS的EDIT程序
This is a DOS based IDE retained from around the mid-90's. Simply enter the following command:
这是 90 年代中期左右保留的基于 DOS 的 IDE。只需输入以下命令:
edit
And EDITwill open in the same DOS window. When you close EDIT, you are returned back to DOS again.
并且EDIT将在同一个 DOS 窗口中打开。当您关闭EDIT 时,您将再次返回到 DOS。
EDITalso works with your mouse.
编辑也适用于您的鼠标。
Once EDITopens, enter the following two lines of code:
一旦EDIT打开后,输入以下代码两行:
@echo off
echo Hello World
Then, click on [File], [Save], type: 'helloworld.bat' in the "File Name" input field, use your mouse to change directories in the "Directories:" pane if you want to, then click [OK]. To return to DOS, click [File], [Exit].
然后,单击[文件],[保存],在“文件名”输入字段中键入:' helloworld.bat',如果需要,请使用鼠标在“目录:”窗格中更改目录,然后单击[确定]]。要返回 DOS,请单击 [文件]、[退出]。
EDIT version 4.5 (I think) was context-sensitive and displayed code using different colours to seperate key word, different data type, symbols etc.
EDIT 4.5 版(我认为)是上下文敏感的,并使用不同的颜色来显示代码来分隔关键字、不同的数据类型、符号等。
(3) Use Windows' built-in Notepad
(3) 使用Windows自带的记事本
This is simple. At the command prompt, enter the following command:
这很简单。在命令提示符下,输入以下命令:
notepad
And Notepad will fire up. It's a simple text editor and does the job when entering small programs.
记事本会启动。它是一个简单的文本编辑器,可以在输入小程序时完成工作。
(4) Use Notepad++. It's FREE!!
(4) 使用记事本++。免费!!
Notepad++is the programmer's choice. It's free, full of useful features and customisable. Find it on the net by searching for "notepad++".
Notepad++是程序员的选择。它是免费的,充满了有用的功能并且可以定制。通过搜索“ notepad++”在网上找到它。
回答by Kelly S. French
From your comment, "Just some knowledge so I can say that I know one way to do programming without IDE" I would say learn to write simple batch files. They can be run from Explorer but they exist as a holdover from the DOS days.
根据您的评论,“只是一些知识,所以我可以说我知道一种无需 IDE 进行编程的方法”,我会说学习编写简单的批处理文件。它们可以从资源管理器中运行,但它们作为 DOS 时代的遗留物而存在。
Start a command prompt window (Start->Run->'cmd'), this will open a window and show a prompt, most likely "c:\" or some other path.
启动命令提示符窗口(开始->运行->'cmd'),这将打开一个窗口并显示一个提示,很可能是“c:\”或其他路径。
Type the following command (followed by )
键入以下命令(后跟 )
echo "Hello World"
You should see:
你应该看到:
"Hello World"
c:\
Now, using whatever editor you'd like, create a text file with that command as the only line. Name the file "hello.bat". When you are at the command prompt you can execute the batch file like so:
现在,使用您喜欢的任何编辑器,创建一个仅包含该命令的文本文件。将文件命名为“hello.bat”。当您在命令提示符下时,您可以像这样执行批处理文件:
c:\hello.bat
"Hello World"
c:\
You have now programmed using the DOS command line. For more commands and such, start with the help system.
您现在已经使用 DOS 命令行进行了编程。有关更多命令等,请从帮助系统开始。
c:\help
Which will display all the available commands for your batch file.
Microsoft has an online reference here.
这将显示批处理文件的所有可用命令。
微软在这里有一个在线参考。
回答by wilhelmtell
DOS is dead for all practical purposes. Under Windows your options boil down to the following:
DOS 在所有实际用途中都已死。在 Windows 下,您的选择归结为以下几点:
- Use an IDE. Visual Studio is one example, Qt another. You can write programs for the commandline with an IDE.
- Use a proper text editor, build tool and other helper tools. You might use gvim for editing code, make for building your project and git for version control. You might as well use the GNU coreutils for other helpers, or maybe even the entire cygwin package.
- 使用 IDE。Visual Studio 是一个例子,Qt 是另一个例子。您可以使用 IDE 为命令行编写程序。
- 使用合适的文本编辑器、构建工具和其他辅助工具。您可以使用 gvim 来编辑代码,使用 make 构建您的项目,使用 git 进行版本控制。您也可以将 GNU coreutils 用于其他帮助程序,甚至整个 cygwin 包。
回答by Umair Khan
Bro, use gcc compiler for which write ur code in any text editor then compile ur code in windows shell or u can say command line environment.
兄弟,使用 gcc 编译器在任何文本编辑器中编写你的代码,然后在 windows shell 中编译你的代码,或者你可以说命令行环境。
Look!
看!
Prompt:/> gcc source_file_name.c
提示:/> gcc source_file_name.c
This command compiles ur code, If there is any error, u will get dispalyed with line numbers, now, every program creates its exe file by the name a.exe by default. To, get the output of ur program,
这个命令编译你的代码,如果有任何错误,你会得到行号的显示,现在,每个程序默认创建名为a.exe的exe文件。为了,得到你的程序的输出,
Prompt:/> a.exe
提示:/> a.exe
O/P
开/关
Hello World!
你好,世界!
To change the name of the exe file there is also a command..
要更改 exe 文件的名称,还有一个命令..