C language programming on Linux

Source: Internet
Author: User

Compile a simple CProgram

(Helloworld, capture errors, compile multiple source files)

The typical example of C language is
Helloworld,

# Include <stdio. h>
Int main (void)
{
Printf ("Hello, world! \ N ");
Return 0;
}

 

We assume thatCodeSave as 'hello. C '. To use
Gcc
Compile the file and run the following command:

$ Gcc-g-wall hello. C-O hello

This command compiles the code in the file 'hello. c' into a machine code and stores it in the executable file 'Hello. The file name of the machine code is
-O
Option. This option is usually used as the last parameter in the command line. If omitted, the default output file is'A. out'.

NotesIf the file with the same name as the executable file already exists in the current directory, it will be overwritten.

Option
-Wall
Enable almost all common warnings of the compiler -- We strongly recommend that you always use this option . The compiler has many other warning options,
-Wall
Is the most common. By default Gcc
No warning information is generated. When writing C
Or C ++
The compiler warning is very helpful for detecting program problems.
Note: Math. h Library and other non- Gcc Standard library called by default. Use -LM Parameters

In this example, the compiler uses
-Wall
No warning is generated because the sample program is completely legal.

Option ""-G ""
Indicates to include debugging information in the generated target file. debugging information can be generated when a program is aborted due to an exception.CoreTo help analyze the source of the error, including many useful information such as the file name and row number of the error.

To run the program, enter the executable file path as follows:

$./Hello
Hello, world!

This loads the executable file into the memory and causes the CPU to start executing its contained commands. Path
./
Indicates the current directory, so
./Hello
Load and execute executable files in the current directory'Hello'.

 

Capture Error

as described above, when C or C ++
is used for programming, compiler warnings are very important assistants. To illustrate this, the following example contains a subtle error: an integer is incorrectly specified with a floating point Controller ' % F' .

# include
int main ( void )
{
printf ( " Two Plus twois % F \ n " ,
4 ) ;
return 0 ;
}

The error is not obvious at a glance, but it can be captured by the compiler, as long as the warning option is enabled
-Wall.

Compile the above program 'bad. c' and get the following message:

$ Gcc-wall bad. C-o bad

Main. C: In the 'main' function:
Main. C: 5: Warning: The format '% F' requires the type 'double', but the type of real parameter 2 is 'int'

 

This indicates that the format string in row 'bad. c' is incorrectly used. GCC messages always have the following format
File Name:Row number:Message. The compiler treats errors differently from warnings. The former blocks compilation, while the latter indicates possible problems but does not block compilation.

In this example, for an integer, the correct format controller should be
% D.

If not enabled
-WallThe program appears to be compiled normally, but will produce incorrect results:

$ Gccbad. C-o bad
$./Bad
Two plus two is 0.000000

Obviously, it is very dangerous to do not check the warning when developing a program. If a function is improperly used, the program may crash or produce incorrect results. Enable the compiler warning option
-Wall
CapturedC
Most Common Errors in programming.

 Compile multiple source files

A source program can be divided into several files. This facilitates editing and understanding, especially when the program is very large. This also makes it possible to compile each part independently.

In the following example
Helloworld
Split3
Files :'Hello. c','Hello_fn.c'And header file'Hello. H'.

This is the first file, that is, the main program 'hello. C ':

# include" hello. H "
int main ( void )
{
Hello ( " world " ) ;
return 0 ;
}

in the previous example, 'Hello. c, we call the library function
printf , in this example, we use a file defined in ' hello_fn.c' Functions in
Hello
replace it.

the main program contains the header file 'Hello. H ', the header file contains the function
Hello
statement. We do not need to go to ' hello. c' the file contains the system header file ' stdio. h' to declare a function
printf , because ' hello. C' no direct call
printf .

This is the second file 'hello. H'. The declaration in the file 'hello. H' only uses one row to specify the function.
Hello
.

VoidHello
(Const
Char *Name);

function
Hello
is defined in the third file ' hello_fn.c' medium:

# include
# include "hello. H "
void Hello ( const
char * name )
{
printf ( " Hello, % s! \ n " , name ) ;
}

Statement
# Include "file. H"
And
# Include <file. h>
Different: the former will first search for files in the current directory before searching for the system header file directory'File. H'The latter only searches for system header files without viewing the current directory.

To useGccCompile the source file and run the following command:

$ Gcc-wall hello. c hello_fn.c-O newhello

In this example, we use
-O
Specifies a different name for the executable file.
Newhello. Note the header file'Hello. H'It is not specified in the command line. The
# Include "Hello. H"
The indicator enables the compiler to automatically include it to a proper position.

To run this program, enter the path name of the executable file:

$./Newhello
Hello, world!

Each part of the source program is compiled into a single executable file, which produces the same result as the previous example.

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.