In the Windows command line to compile and run C / C ++ program (reprint)

This article is reproduced in: https: //www.cnblogs.com/-beyond/p/6636485.html



Location demo.cpp file in the C: \ Users \ Administrator \ Desktop directory

The first step: In the DOS interface into this directory

Step 2: g ++ plus the file name, pay attention to add the file extension, it will generate an executable file of a a.exe (Windows default generate a.exe, Linux generated by default a.out)

This is because C ++ source code, so use the g ++ command, if the C language source code, you can either use the g ++, GCC may be used (or cc) command.

The third step: direct input a.exe and press Enter to execute the program generated by the compiler demo.cpp link (Linux environment, then change ./a.out run the program)

The fourth step: At this time, i.e., input parameters a and b values, the result after pressing the return to

 Step 5: Ctrl + z terminate the program

 

Read the above process, you may feel inconvenient,

1-> resulting executable file is a.exe and not want to get demo.exe

2-> every each test data have to be entered manually, too troublesome

For a question:

First, you can add the optional second step above the g ++ demo.cpp -o

Use g ++ -o demo.exe demo.cpp (or g ++ -o demo demo.cpp) to generate executable demo.exe,

If you want to be named test.exe just g ++ -o test demo.cpp (or g ++ -o test demo.cpp) to

For Question two:

Run the program still can demo.exe command, but this time had a one of the input test data

For convenience, the test data may be previously stored in a text file (assumed to be input.txt) inside,

Running time data can be read directly entered, noted that the format of the data format to be inside input.txt prescribed by demo.exe

The result is then written to a text file to another (assuming output.txt), the direct view

Use demo.exe <input.txt> output.txt to the above operation

 

expand:

If you take a closer look at the source code to save folder, you will find that when we run with a key to compile IDE system, there will be * .o files, but this time with the command-line compiler run without generating * .o file, if you want * .o files, then you need to add a -c option at compile time, this will only generate a * .o files * .o files can be extracted, formed into a library file 

This article is reproduced in: https: //www.cnblogs.com/-beyond/p/6636485.html



Location demo.cpp file in the C: \ Users \ Administrator \ Desktop directory

The first step: In the DOS interface into this directory

Step 2: g ++ plus the file name, pay attention to add the file extension, it will generate an executable file of a a.exe (Windows default generate a.exe, Linux generated by default a.out)

This is because C ++ source code, so use the g ++ command, if the C language source code, you can either use the g ++, GCC may be used (or cc) command.

The third step: direct input a.exe and press Enter to execute the program generated by the compiler demo.cpp link (Linux environment, then change ./a.out run the program)

The fourth step: At this time, i.e., input parameters a and b values, the result after pressing the return to

 Step 5: Ctrl + z terminate the program

 

Read the above process, you may feel inconvenient,

1-> resulting executable file is a.exe and not want to get demo.exe

2-> every each test data have to be entered manually, too troublesome

For a question:

First, you can add the optional second step above the g ++ demo.cpp -o

Use g ++ -o demo.exe demo.cpp (or g ++ -o demo demo.cpp) to generate executable demo.exe,

If you want to be named test.exe just g ++ -o test demo.cpp (or g ++ -o test demo.cpp) to

For Question two:

运行程序仍可以用demo.exe命令,但是此时还得一条一条的输入测试数据

但为了方便,可以将测试数据事先保存在文本文件(假设为input.txt)里面,

运行的时候直接往里面读取数据即可,注意的是input.txt里面的数据格式要按照demo.exe规定的格式

然后将结果写入另外一个文本文件(假设为output.txt)中,直接查看

使用demo.exe <input.txt> output.txt即可完成以上操作

 

拓展:

如果你仔细看一下保存源代码的文件夹,你就会发现,当我们用IDE集成环境一键编译运行的时候,会出现 *.o文件,而此时用命令行编译运行没有生成 *.o文件,如果你想要 *.o 文件,那么你就需要在编译的时候添加一个 -c选项,这样就会只生成一个 *.o文件, *.o文件可以抽取出来,做成库文件 

Guess you like

Origin www.cnblogs.com/yxyun/p/11463158.html
Recommended