C & Cpp Program Development in Linux (1), Linux
I have been designing programs in Windows. I recently started to learn how to use Linux, so I plan to transfer program development to Linux. Today, I will briefly introduce the C program development steps under the system.
First, install the vim and gcc tools in advance, and then compile a "Hello World" program:
1. Open the command window in the directory, enter vim hello. c to create and open the hello. c file, press <I> to enter the editing mode, and enter the following code:
1 #include<stdio.h>2 3 int main()4 {5 printf("Hello World!\n");6 return 0;7 }
Press <Esc> to exit editing. <:> wq: Save and exit the editing page.
2. Input gcc hello. c-o hello to compile the hello. c file and output the hello executable file. It means: gcc [source file name] outputs [output file name]. You can also perform step-by-step compilation.
Gcc-E hello. c-o hello. I pre-compiled
Gcc-S hello. I-o hello. s compiles the generated hello. I file to generate assembly code.
Gcc-c hello. s-o hello. o compiles the assembly code file hello. s into the target file.
Gcc hello. o-o hello connects hello. o to the C standard input and output library, and finally generates the program hello
3. Enter./hello to run the hello program in the current directory. The "Hello World!" printed on the screen will be displayed !" .
Now that the Hello World Program has been compiled, I will introduce the compilation of multiple files. Create Main. c and Add. c files
1 /*2 Add.c3 */4 #include<stdio.h>5 6 int add(int num0,int num1)7 {8 return num0+num1;9 }
1 /* 2 Main.c 3 */ 4 #include<stdio.h> 5 6 int add(int num0,int num1); 7 8 int main() 9 {10 printf("%d",add(1,2));11 return 0;12 }
After writing two files, enter gcc Main. c Add. c-o ADD in the command line. Enter./ADD to run the program.