Compile and run the C/C ++ program on the command line interface.
Compile and run the C/C ++ program on the command line interface
After compiling the program, you need to compile and execute the program. Many compilers have integrated development environments (IDES), which are very useful when developing large programs. However, when the program is small, it will be very troublesome to use IDE. For example, if you use IDE To write a Bubble Sorting Algorithm to sort an array, you need to add source files, compile programs, compile, debug, and execute in a new project or project, just a simple bubble sort, you need to perform various operations on the IDE for a longer time than the Write Program. Many PCs may start Visual Studio for a long time, which makes it inconvenient to use the IDE. Especially for beginners of C/C ++, they only need to pay attention to the language itself, rather than some development tools. The complicated menu functions of IDE are a great headache for users. In this regard, it is very convenient to compile and run the program through the command line interface.
Take the GCC compiler as an example:
Many of my friends use Windows, while the GCC compiler is the default compiler for Linux. If they do not want to change the system, they also want to use the powerful GCC, you can install MinGW (Minimalist GNU for Windows ).
MinGW 4.8.1 download link (Baidu cloud ):
Link: http://pan.baidu.com/s/1gfJmMXl password: bniv
Decompress the compressed file to drive D (other file directories are also supported. Take drive D as an example) and add the environment variable "D: \ mingw32 \ bin ".
View the GCC version, run cdm, and enter the command: ($ is a system prompt)
$ Gcc-v
The following uses the Bubble sorting program as an example to describe how to use the command line interface.
Create the C ++ source file test under "D: \ myCode. cpp (create a TXT file, change the file name and suffix), and open test with Sublime Text (or Notepad ++, gedit, and other Text editors. cpp:
1 #include <iostream> 2 using namespace std; 3 void bubbleSort(int *arr, int len) 4 { 5 int temp; 6 if (len < 2) 7 return; 8 for (int i = 1; i < len; ++i) { 9 for (int j = 0; j < len - i; ++j) {10 if (arr[j] > arr[j + 1]){11 temp = arr[j];12 arr[j] = arr[j + 1];13 arr[j + 1] = temp;14 }15 }16 }17 }18 int main()19 {20 int arr[10] = { 34, 11, 6, 89, 1, 60, 7, 901, 33, 52 };21 bubbleSort(arr, 10);22 cout << "Bubble Sort Result:" << endl;23 for (int i = 0; i < 10; ++i)24 cout << arr[i] << " ";25 cout << endl;26 }
If the program requires other custom header files, put them in the same directory as the source files.
Open the command window in "D: \ myCode \" and type the command:
$ G ++ test. cpp
If there is no error in the program, the g‑executable command will compile the test.cpp program and generate the executable program a.exe. To customize the name of the executable file, enter the following command:
$ G ++ test. cpp-o test.exeOr$ G ++ test. cpp-o test
In this example, the executable program test.exe is generated.
What if I want to compile a C program? Replace g ++ of the compilation command with gcc.
In Windows, run the following command to view the program return status:
$ Echo % ERRORLEVEL %
If the generated file is test.exe, type the command in the command line:$ Test.exeOr$ TestTo run the program.
In addition, if Visual Studio (such as VS2010 or VS2013) is used, the compilation command is:
$ Cl/ehstest. cpp
The command will generate the executable program test.exe.
How can I use the C ++ 11 standard supported by the compiler? Enter the following command:
$ G ++ test. cpp-std = c ++ 11Or$ G ++ test. cpp-o test-std = c ++ 11