简体   繁体   English

我们如何在命令行开发环境中编写程序?

[英]How do we write a program in Command line development environment?

我一直在IDE中编写代码,我刚刚读到还有一个命令行开发环境,其中的代码是用DOS编写的。我在Google上进行了搜索,但没有找到有关如何使用命令行开发环境的结果。我的操作系统是Windows XP。非常感谢您为我在DOS中编写hello world程序提供帮助,并解释了如何运行它。

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). 您只需使用您喜欢的任何文本编辑器来创建C源文件,然后调用编译器命令行来编译和链接程序(通常,IDE正是这样做的,但是在幕后)方式)。 How the command line is invoked depends on the exact toolchain you're using. 命令行的调用方式取决于您所使用的确切工具链。

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). 您可能还需要为特定的编译器工具链设置环境(可能需要设置正确的路径和各种其他env变量)。

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可以帮助您管理可能需要使用的选项的复杂性。

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. 从您的评论中,“只要了解一些知识,我就可以说我知道不使用IDE进行编程的一种方式”,我会说学习编写简单的批处理文件。 They can be run from Explorer but they exist as a holdover from the DOS days. 可以从资源管理器中运行它们,但是从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". 将文件命名为“ hello.bat”。 When you are at the command prompt you can execute the batch file like so: 当您在命令提示符下时,可以像这样执行批处理文件:

c:\hello.bat
"Hello World"
c:\

You have now programmed using the DOS command line. 现在,您已使用DOS命令行进行了编程。 For more commands and such, start with the help system. 有关更多命令等,请从帮助系统开始。

c:\help

Which will display all the available commands for your batch file. 这将显示批处理文件的所有可用命令。
Microsoft has an online reference here . Microsoft 在此处有在线参考。

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中输入代码(请参见下面的“编辑”)。


(1) You can send keystrokes directly to a file (1)您可以将击键直接发送到文件

You do this by redirecting output to CON (the console) to a file. 您可以通过将输出重定向到CON (控制台)到文件来实现。 The only oddity of this method is that you end the 'session' by entering a CTRL-Z when you are finished. 这种方法的唯一奇怪之处是,您在完成操作时通过输入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. 通常在所有批处理文件的开头都可以找到“ @echo off ”。 It simply instructs the command interpretter NOT to display each command as it is being executed (or parsed). 它只是指示命令解释器在执行(或分析)命令时不要显示每个命令。

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-Z key combination (don't forget to press the ENTER key as well). 当您按下CTRL-Z组合键时,将显示' ^ Z '(不要忘记也按下ENTER键)。

When you press the ENTER key 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. 这是大约90年代中期以来基于DOS的IDE。 Simply enter the following command: 只需输入以下命令:

   edit

And EDIT will open in the same DOS window. 并且EDIT将在同一DOS窗口中打开。 When you close EDIT , you are returned back to DOS again. 当您关闭EDIT时 ,您将再次返回DOS。

EDIT also works with your mouse. 编辑也可以使用鼠标。

Once EDIT opens, 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 ]. 然后,单击[ 文件 ],[ 保存 ],在“ 文件名 ”输入字段中键入:“ helloworld.bat ”,如果需要,使用鼠标在“ 目录: ”窗格中更改目录,然后单击[ 确定]。 ]。 To return to DOS, click [ File ], [ Exit ]. 要返回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++. (4)使用记事本++。 It's FREE!! 免费!!

Notepad++ is the programmer's choice. Notepad ++是程序员的选择。 It's free, full of useful features and customisable. 它是免费的,具有许多有用的功能并且可以自定义。 Find it on the net by searching for " notepad++ ". 通过搜索“ notepad ++ ”在网上找到它。


DOS is dead for all practical purposes. 出于所有实际目的,DOS已死。 Under Windows your options boil down to the following: 在Windows下,您的选择可归结为以下内容:

  • Use an IDE. 使用IDE。 Visual Studio is one example, Qt another. Visual Studio是一个示例,Qt是另一个示例。 You can write programs for the commandline with an IDE. 您可以使用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. 您可能使用gvim来编辑代码,使用make来构建您的项目,使用git来进行版本控制。 You might as well use the GNU coreutils for other helpers, or maybe even the entire cygwin package. 您最好将GNU coreutils用于其他帮助程序,甚至可能用于整个cygwin软件包。

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. 此命令将编译您的代码,如果有任何错误,则会显示行号,现在,每个程序默认情况下都会以名称a.exe创建其exe文件。 To, get the output of ur program, 要获取ur程序的输出,

Prompt:/> a.exe 提示:/> a.exe

O/P O / P

Hello World! 你好,世界!

To change the name of the exe file there is also a command.. 要更改exe文件的名称,还有一个命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写程序以使用命令行中指定的字符交换输入文件中的字符? - How do I write a program to swap a character in the input file with a character specified on the command line? 如何使用文件,环境和/或命令行初始化C程序? - How to initialise a C program using file, environment and/or command line? 如何从命令行编写一个 c 程序,它将 2 个字符串作为 arguments? - How to write a c program that takes 2 strings as arguments from the command line? 如何在* C *中围绕命令行程序编写GUI包装? - How to write a GUI wrapper around a command line program in *C*? 通过命令行在Cygwin环境中启动C程序 - Launch C program in Cygwin Environment through command line 如何编写 C 程序以使用 linux 使用命令行替换字符 - How to Write a C program to Replace characters using command line using linux 如何编写从命令行获取整数的C程序? - How could I write a C program that takes an integer from command line? 在Linux中用交互式命令行界面编写C守护程序类型程序 - Write a C daemon type program with an interactive command line interface in linux 编写一个小程序,sleepy,从命令行获取循环计数 - Write a small program, sleepy, that gets a loop count from the command line 是否可以从命令行写入打开的 pipe(来自 C 程序)? - Is it possible to write to an open pipe (from a C program) from the command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM