GCC programming Basics

Source: Internet
Author: User

Gccgnu c Compiler)

If an error occurs in Linux for the first time, please leave a message to specify criticism.

1. gcc can compile executable programs on multiple hardware platforms, and its execution efficiency is 20% to 30% higher than that of the general compiler.

2. The GCC compiler can compile and link source programs and assembler programs in C and C ++ into executable files.

3. in Linux, the executable file does not have a uniform suffix. The system distinguishes the executable file from the non-Executable File Based on the file attributes.

GCC uses suffixes to differentiate file categories:

. C: C language source code file

.: Library file consisting of the target file

. C. Cc,. cxx: C ++ source code file

. H: Header file

. I: C source code files that have been preprocessed

. Ii: C ++ source code files that have been preprocessed

. O: Target file after compilation

. S: Assembly language code file

. S: Compiled assembly language code files

First Program

Hello. c

#include<stdio.h>int main(void){    printf("hello world!\n");    return 0; }

Compile command

# Gcc hello. c-o hello

Run commands

#./Hello

Running result:

Hello world!


Gcc basic usage: gcc [options] [filename]

Options: Compilation options required by the compiler

Filename: name of the file to be compiled

-OOutput_filename: Compile the name of the executable file. If this option is not specified, the system will provide the preset Executable File a. out.

-COnly compile and not connect to an executable file, and generate the target file with the. o suffix.

-GGenerate the necessary symbolic information for the debugging tool. This option is required for program debugging.

-OThe program execution speed will be greatly improved after the program compilation and connection process is optimized and optimized)

-I dirname: Add the directory specified by dirname to the program header file directory list.

The C program header file contains two scenarios: # include <x. h>

# Include "x. h"

For the <> Preprocessing Program, search for the corresponding files in the system preset header file directory, such as/usr/include)

"" If the required file is not found in the current directory, search for it in the specified dirname directory.

-L dirname:Add the directory specified by dirname to the library file directory list. In the default state, The Connection Program ld searches for the required library files in the system's preset path, such as/usr/lib) first in the directory specified by-L, then go to the system preset path to search for it.

-LConnect to libraries in lib

-Static: Static Link Library File

The library is divided into dynamic library and static library. The dynamic library uses the. so suffix static. a suffix.

When using the static library, find the required functions and the program copies them to the executable files.

The dynamic library leaves a "flag" in the executable program. When the program is running, it loads the dynamic library function.

Dynamic library is selected by default when linking in linux

For example, gcc-static hello. c-o hello

-WallGenerate all warning information

-WDo not generate warning

-DPre-compilation options





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.