2. Create folders to hold source files
mkdir HelloWorld//Create Folder command
CD HelloWorld//into the new folder, it should be said that the directory is better, Windows used to
Vim MAIN.C//Open vim and create Main.c file
Press the I key to enter the editing state, the following is the program
#include
int main ()
{
printf ("Hello world!/n");
return 0;
}
Press ESC to enter command mode and save as ZZ to exit
GCC main.c//compile
./a.out//Run, generate a.out executable file by default
How to compile a C + + program and run a collection in gcc++
GCC is capable of compiling three languages: C, C + +, and object C (a type-oriented extension of the C language). Use the GCC command to compile and connect C and C + + source programs at the same time.
#DEMO #: HELLO.C
If you have two or a few C source files, you can also easily compile, connect, and build the executable file with GCC. For example, suppose you have two source files Main.c and factorial.c two source files, and now you want to compile a program that calculates the factorial.
-----------------------
List FACTORIAL.C
-----------------------
#include <stdio.h>
#include <stdlib.h>
int factorial (int n)
{
if (n <= 1)
return 1;
Else
return factorial (n-1) * n;
}
-----------------------
-----------------------
List MAIN.C
-----------------------
#include <stdio.h>
#include <stdlib.h>
int factorial (int n);
int main (int argc, char **argv)
{
int n;
if (ARGC < 2) {
printf ("Usage:%s n/n", argv [0]);
return-1;
}
else {
n = atoi (argv[1]);
printf ("Factorial of%d is%d./n", N, factorial (n));
}
return 0;
}
-----------------------
Use the following command to compile the build executable and execute the program:
$ gcc-o factorial main.c factorial.c
$./factorial 5
Factorial of 5 is 120.
GCC can be used to compile both C and C + + programs. In general, the C compiler uses the suffix name of the source file to determine whether it is a C program or a C + + program. In Linux, the C source file has the suffix name. C, and the C + + source file has a suffix named. C or. cpp.
However, the GCC command compiles only C + + source files and cannot be automatically connected to libraries used by C + + programs. Therefore, you typically use the g++ command to complete the compilation and connection of C + + programs that automatically invoke the GCC implementation for compilation. Suppose we have a C + + source file (hello) as follows. C):
#include <iostream.h>
void Main (void)
{
cout << "Hello, world!" << Endl;
}
You can call the g++ command to compile, connect, and build the executable as follows:
$ g++-o Hello hello. C
$./hello
Hello, world!.
Main options for 1.7.2 Gcc/egcs
Table 1-3 Common options for GCC commands
Option explanation
-ansi only supports the ANSI standard C syntax. This option will prohibit certain features of GNU C,
such as ASM or typeof keywords.
-C compiles and generates only the target file.
-DMACRO defines macro macros with the string "1".
-DMACRO=DEFN defines macro macros with the string "DEFN".
-E only runs the C precompiled compiler.
-G generates debug information. The GNU debugger can take advantage of this information.
-idirectory specifies additional header file search paths for directory.
-ldirectory Specifies additional library search paths for directory.
-llibrary searches for the specified library of libraries when connected.
-m486 code optimization for 486.
-o file to generate the specified output files. Used when generating an executable file.
-o0 is not optimized for processing.
-O or-o1 optimization generates code.
-o2 further optimization.
-o3 is further optimized than-O2, including the inline function.
-shared generate a shared destination file. Typically used when building a shared library.
-static prohibit the use of shared connections.
-umacro cancels the definition of macro macros.
-W does not generate any warning messages.
-wall generates all warning messages.
Using VIM to write C programs under Linux