Linux under the command line to compile the C program

Source: Internet
Author: User
-------------------------------------------

Linuxmine Collection and collation Author: Linux Treasure House (http://www.linuxmine.com) Time: 2006-10-11

-------------------------------------------


C is the system language on all versions of UNIX.

C has made great progress in the past 20 years. In the late 80, the American National Standards Association (American Nationalstandards Institute) released a C language standard called ANSI C. This more guarantees the future consistency of C on different platforms. In the 80 's there was also a C-object-oriented extension called C + +.

The C compiler available on Linux is the GNU C compiler, which builds on the Free Software Foundation's programming license and is therefore free to release. You can find it on the Linux release CD.

The GNU C compiler (GCC) issued with Slackware Linux is a fully functional ANSI C-compliant compiler. If you are familiar with a C compiler on another operating system or hardware platform, you will soon be able to master GCC. This and Java are at the command line

The compilation is similar and we'll explain how to use GCC and some of the GCC compilers ' most common options below.

Format gcc [option] [sourceFileName]

GCC options are very wide, about hundreds, but usually not very much, we will introduce some of the most commonly used options:

Simplest of all: gcc hello.c

By default, a.out executable files are generated, and you only need to enter them on the terminal./a.out can see the results of the execution.

If you want to specify the name of the generated target file, then you can add the-o option, which commands the following:

Gcc-o Hello hello.c

GCC also allows you to only compile the source program, so that you can only check the compile-time errors, the benefit of debugging programs, with the-C option to achieve this goal

The order is as follows:

gcc-c Hello hello.c

The result generates an intermediate code hello.o file

The compile option tells GCC to stop compiling after generating assembly language files for C code. The default extension of the assembly language file produced by GCC is. S. The-e option instructs the compiler to preprocess only the input files. When this option is used, the output of the preprocessor is sent to the standard output instead of being stored in the file.

You can then link to the above intermediate code and use the command:

Gcc-o hello.out hello.c

Optimization options

When you compile C code with GCC, it tries to compile with minimal time and make the compiled code easy to debug. Easy debugging means that the compiled code has the same order of execution as the source code, and the compiled code is not optimized. There are many options available to tell GCC to generate smaller, faster executables on a more compile-time basis and at the expense of easy debugging. The most typical of these options are the-O and-O2 options.

The-o option tells GCC to base optimization on the source code. These optimizations make the program perform faster in most cases. The-O2 option tells GCC to produce as little and as fast code as possible. The-O2 option will make compilation slower than when using-O. Often, however, the resulting code executes more quickly.

GCC supports a number of debugging and profiling options. The G-and-PG options are most commonly used in these options.

Option tells GCC to generate debug information that can be used by the GNU debugger to debug your program. GCC provides a feature that is not available in many other C compilers where you can use the-G and-O (generating optimized code). This is useful because you can debug your code as closely as possible with the final product. When you use both options you must be aware that some of the code you write has been modified by GCC during optimization. For more information about debugging C programs, see the next section, "Debug C programs with GDB."

as follows command Gcc-c-G hello.c

The PG option tells GCC to add extra code to your program, and then generate gprof profiling information to show how time-consuming your program is.

More information about GGC can be viewed in man gcc


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.