Use Vim to write C language programs in Linux

Source: Internet
Author: User

1. Enter the character page

2. Create a folder to store the source file

Mkdir helloworld // create a folder

CD helloworld // enter the new folder. Here we should say that the directory is better, and Windows is used to it.

Vim main. C // open vim and create the main. c file

// Press the I key to enter the editing status. The following are the programs.

# Include
Int main ()

{

Printf ("Hello world! /N ");

Return 0;

}

Press ESC to Enter command mode, press ZZ to save and exit

GCC main. C // compile

./A. Out // run. The a. out executable file is generated by default.
How to compile a C ++ program in GCC ++ and run the favorites
GCC can compile three languages: C, C ++, and Object C (an object-oriented extension of C ). The GCC command can be used to compile and connect the C and C ++ source programs at the same time.

# DEMO #: Hello. c

If you have two or a few C source files, you can easily use GCC to compile, connect, and generate executable files. For example, if you have two source files main. C and factorial. C, compile a program to calculate 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;
}
-----------------------

-----------------------
Listing 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;
}
-----------------------

The following command can be used to compile and generate executable files and execute programs:
$ 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. Generally, the C compiler uses the extension of the source file to determine whether it is a C program or a C ++ program. In Linux, the extension of the C source file is. C, and the extension of the C ++ source file is. C or. cpp.


However, the GCC command can only compile the C ++ source file, but cannot automatically connect to the library used by the C ++ program. Therefore, the G ++ command is usually used to compile and connect the C ++ program. The program automatically calls GCC for compilation. Suppose we have the following C ++ source file (hello. C ):


# Include <iostream. h>

Void main (void)
{
Cout <"Hello, world! "<Endl;
}

You can call the G ++ command to compile, connect, and generate an executable file as follows:

$ G ++-O hello. c
$./Hello
Hello, world!

1.7.2 main gcc/egcs options
Table 1-3 common GCC commands
Option description
-ANSI only supports the ANSI standard C syntax. This option will disable some features of gnu c,

For example, ASM or typeof keywords.
-C only compiles and generates the target file.
-Dmacro defines the macro with the string "1.
-Dmacro = defn: Define the macro with the string "defn.
-E only runs the C pre-compiler.
-G generates debugging information. The GNU Debugger can use this information.
-Idirectory: specify an additional header file to search for the path directory.
-Ldirectory: specify an additional function library to search for the path directory.
-Search for the specified library when connecting to llibrary.
-Msung optimizes code for 486.
-O file: generate the specified output file. Used to generate executable files.
-O0 is not optimized.
-O or-O1 optimized code generation.
-O2 is further optimized.
-O3 is further optimized than-O2, including the inline function.
-Shared shared object generation. It is usually used to create a shared library.
-Static prohibit the use of shared connections.
-Umacro undefines macro macros.
-W does not generate any warning information.
-Wall generates all warning information.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.