Linux under the introduction of GCC programming __ block Chain

Source: Internet
Author: User
statement:This article is from: Ubuntu Chinese Forum

This article is a primer for GCC beginners, so the content is simple. If you know the following 3 commands can compile C + +, you do not have to waste time in this article
Code:g++-wall Hellocpp.cpp
Gcc-wall hellocpp.cpp-lstdc++
Gfortran-wall hellocpp.cpp-lstdc++

Note: The latest version of this article is http://wiki.ubuntu.org.cn/Gcchowto in the wiki (it is recommended that you directly access the version in the wiki)
If you want to find an integrated development environment, you can access Http://wiki.ubuntu.org.cn/C_Cpp_IDE
If you want to build a GTK or QT compilation environment, you can access HTTP://WIKI.UBUNTU.ORG.CN/GTK and QT compilation environment installation and configuration


Note: Make sure that the Build-essential package is installed before you start

Compile a simple C program

The introductory example of the C language Classic is Hello world, and here is a sample code:


Code:#include <stdio.h>
Int
Main (void)
{
printf ("Hello, world!\n");
return 0;
}

We assume that the code is stored as a file ' hello.c '. To compile the file with GCC, use the following command:

Code:$ gcc-wall Hello.c-o Hello

This command compiles the code in the file ' hello.c ' into machine code and stores it in the executable ' hello '. The name of the machine code is specified by the-o option. This option is often used as the last parameter in the command line. If omitted, the output file defaults to ' A.out '.

Note that if a file with the same name as the executable file already exists in the current directory, it will be covered.

Option-wall turn on almost all of the compiler's usual warnings--it is strongly recommended that you always use this option. The compiler has many other warning options, but-wall is the most common. By default, GCC does not generate any warning messages. Compiler warnings are very helpful when writing C or C + + programs to detect problems with your program.

In this case, the compiler uses the-wall option without warning because the sample program is completely legal.

to run the program, enter the path to the executable file as follows:
Code:$./hello
Hello, world!.
This loads the executable file into memory and causes the CPU to start executing the instructions it contains. Path./refers to the current directory, so./hello loads and executes the executable file ' Hello ' in the current directory.

Catching errors

As mentioned above, compiler warnings are important assistants when programming in C or C + +. To illustrate this, the following example contains a subtle error: a floating-point control character '%f ' is incorrectly specified for an integer value.

Code:#include <stdio.h>

Int
Main (void)
{
printf ("Two plus two is%f\n", 4);
return 0;
}

At first glance the error is not obvious, but it can be caught by the compiler as long as the warning option is enabled-wall.

Compiling the above program ' BAD.C ' will get the following message:

Code:$ Gcc-wall Bad.c-o Bad
bad.c:in function ' main ':
Bad.c:6: warning:double format, different type arg (ARG 2)

This indicates that the format string is not used correctly in line 6th of the file ' BAD.C '. GCC messages always have the following format file name: line number: message. The compiler treats errors differently from warnings, which block compilation, which indicates possible problems but does not prevent the program from compiling.

In this example, the correct format control character for an integer value should be%d.

If you do not enable-wall, the program surface appears to compile correctly, but produces incorrect results:

Code:$ gcc Bad.c-o Bad
$./bad
Two plus two is 2.585495

Obviously, it is very dangerous to develop programs without checking for warnings. If a function is improperly used, it can cause the program to crash or produce an incorrect result. Turn on the compiler warning option-wall to capture most common errors in C programming.

Compiling 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 for individual parts to compile independently.

In the following example, we split the program Hello world into 3 files: ' Main.c ', ' hello_fn.c ' and header file ' Hello.h '. This is the main program ' MAIN.C ':

Code:#include "hello.h"
Int
Main (void)
{
Hello ("World");
return 0;
}

In the previous example ' hello.c ', we call the library function printf, in this case we replace it with a function, defined in the file ' Hello_fn.c '.

The main program contains a header file ' Hello.h ', which contains the declaration of the function hello. We do not need to include the system header file ' stdio.h ' in the ' main.c ' file to declare the function printf because ' main.c ' does not call printf directly.

The declaration in the file ' Hello.h ' uses a single line to specify the prototype of the function hello.

Code:void Hello (const char * name);

The definition of the function hello is in the file ' hello_fn.c ':

Code:#include <stdio.h>
#include "hello.h"

void
Hello (const char * name)
{
printf ("Hello,%s!\n", name);
}

The statement #include "FILE.h" differs from the #include <FILE.h>: The former searches for the file ' FILE.h ' in the current directory before searching the system header file directory, and then searches for the system header file without viewing the current directory.

To compile the above source file with GCC, use the following command:

Code:$ gcc-wall main.c Hello_fn.c-o Newhello

In this case, we use Option-O to specify a different name for the executable file Newhello. Note that the header file ' hello.h ' is not specified on the command line. The #include "hello.h" designator in the source file enables the compiler to automatically include it in the appropriate location.

To run this program, enter the path name of the executable file:
Code:$./newhello
Hello, world!.

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

Link External Library

A library is a collection of precompiled target files (object files) that can be linked into a program. A special archive file (archive file) store with a static library suffix of '. a '.

Standard system libraries can be found in the directory/usr/lib and/lib. For example, in Unix-like systems, the C-language math library is typically stored as a file/USR/LIB/LIBM.A. The prototype of a function in this library is declared in the header file/USR/INCLUDE/MATH.H. The C standard library itself is stored as/USR/LIB/LIBC.A, which contains functions specified by the Ansi/iso C standard, such as ' printf '. For every C program, LIBC.A is linked by default.

The following is an example of calling the sin function in the math library LIBM.A:

Code:#include <math.h>
#include <stdio.h>

Int
Main (void)
{
Double x = sin (2.0);
printf ("The Value of Sin (2.0) is%f\n", x);
return 0;
}

Attempting to generate an executable file separately from this file will result in a link-phase error:
Code:$ Gcc-wall Calc.c-o Calc
/tmp/cckdhfi8.o:in function ' main ':
CALC.C: (. text+0x1b): Undefined reference to ' sin '

The sin, which is not defined in this program nor is in the default library ' LIBC.A ', is not linked to ' LIBM.A ' unless specified.

In order for the compiler to link sin to the main program ' CALC.C ', we need to provide the math library ' LIBM.A '. An easy to think but troublesome approach is to specify it explicitly at the command line:

Code:$ Gcc-wall Calc.c/usr/lib/libm.a-o Calc

The function library ' LIBM.A ' contains the target files for all mathematical functions, such as Sin,cos,exp,log and sqrt. The linker will search all files to find the target file that contains sin.

Once the target file containing sin is found, the main program can be linked, and a complete executable file can be generated:

Code:$./calc
The value of sin (2.0) is 0.909297

The executable file contains the machine code of the main city and the machine code corresponding to the sin in the function library ' LIBM.A '.

To avoid specifying a long path on the command line, the compiler provides a quick option '-l ' for the linked function library. For example, the following command

Code:$ Gcc-wall Calc.c-lm-o Calc

Equivalent to the command we specified above for the library full path '/USR/LIB/LIBM.A '.

In general, option-l name causes the linker to attempt to link function library file Lib NAME. A in the System library directory. A large program usually uses many-l options to specify the math library, graphics library, and network library to link to.


Compiling C++/fortran

As you may know: GCC is the acronym for the GNU Compiler Collection (GNU Compiler Collection). It can compile C,c++,objective-c,fortran,java and Ada languages.

We are only involved in the C language, then how to compile other languages with GCC.

Compiling C + +

(The following two commands are equivalent, but I believe you will choose the former ^_^)
Code:g++-wall Hello.cpp-o Hello
Gcc-wall Hello.cpp-o hello-lstdc++

Compiling FORTRAN

If your GCC version is >=4.0, either of the following commands can be selected
Code:Gfortran-wall Hello.f-o Hello
Gcc-wall Hello.f-o Hello-lgfortran-lgfortranbegin

If the GCC version is <4.0, then the following command is optional
Code:G77-wall Hello.f-o Hello
Gcc-wall Hello.f-o hello-lfrtbegin-lg2c

For Java and Ada programming, the former is recommended to use SUN-JAVA6-JDK, the latter is not very common, this article as an introductory article, it is not involved.

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.