Compiling the program with GCC

Source: Internet
Author: User

On a Linux system, a file can be executed to see if there is a permission (x) to execute, but the executable that really knows on the Linux system is actually a binary file (binary program), such as/usr/bin/passwd These files are binary program code.

How do you create an executable binary program? First write the program, using the word processor to write the program is the source code, this source code is a generic plain text document. After writing the source code, the program code is compiled into binary programs that the operating system can understand. Compilation requires the compiler to act, and after compiling and linking the compiler, an executable binary program can be generated. For example, the most standard programming language on Linux is C, we use C to write the source code, compile it with the standard C language compiler in Linux, and then build the executable binary program.

Sometimes we refer to other external subroutines in the program, or use the functions provided by other software, we must add the function library in the process of compiling, so that the compiler can make all the program code and the function library as a link to produce the correct execution file (executable binary program file).

    • Make and configure

    • Tarball

  Tarball file, in fact, is the software all the original code files are first packaged in tar, and then compression technology to compress, the most common is gzip, so the tarball file extension is *.tar.gz or *tgz. Because the compression efficiency of the bzip2 is better, the crotch name will also become *.tar.bz2.

    • Print Hello World

(1) Compile the original code directly with GCC

[[Email protected]]#VIHello.c[[email protected] documents]#CatHello.c#include<stdio.h>intMain (void) {printf ("Hello world\n");} [[Email protected]]#GCChello.c[[email protected]]# lltotal A-rwxr-xr-x.1Root root4643June - xx: -a.out #编译成功的可执行binary Program-rw-r--r--.1Root root theJune - xx: -hello.c[[email protected]]#./a.out #执行文档Hello World

(2) Generate the target file for other actions, and do not use the default file name A.out

[[Email protected]]#GCC-c hello.c [[email protected]]# ll Hello*-rw-r--r--.1Root root theJune - xx: -hello.c-rw-r--r--.1Root root852June -  on:xxhello.o #产生的目标文件 [[Email protected]]#GCC-o Hello hello.o[[email protected]]# lltotal --rwxr-xr-x.1Root root4643June -  on:xxHello #可执行文件-rw-r--r--.1Root root theJune - xx: -hello.c-rw-r--r--.1Root root852June -  on:xxhello.o
[Email protected] documents]#./hello
Hello World

(3) Compilation of sub-programs

[[Email protected]]#VIThanks.c[[email protected]]#Catthanks.c #include<stdio.h>intMain (void) {printf ("Hello world\n");   Thanks_2 (); #子程序}[[email protected]]#VIThanks_2.c[[email protected]]#CatThanks_2.c#include<stdio.h>void thanks_2 (void) {printf ("Thank you!\n");} [Email protected]]# ll Thanks*-rw-r--r--.1Root root inJune -  on: tothanks_2.c-rw-r--r--.1Root root theJune -  on:GenevaThanks.c[[email protected]]#GCC-c THANKS.C thanks_2.c [[email protected]]# ll Thanks*-rw-r--r--.1Root root inJune -  on: tothanks_2.c-rw-r--r--.1Root root856June -  on: toTHANKS_2.O-rw-r--r--.1Root root theJune -  on:GenevaTHANKS.C-rw-r--r--.1Root root892June -  on: toThanks.o[[email protected]]#GCC-o Thanks thanks.o thanks_2.o[[email protected]]# ll Thanks*-rwxr-xr-x.1Root root4740June -  on: .Thanks-rw-r--r--.1Root root inJune -  on: tothanks_2.c-rw-r--r--.1Root root856June -  on: toTHANKS_2.O-rw-r--r--.1Root root theJune -  on:GenevaTHANKS.C-rw-r--r--.1Root root892June -  on: tothanks.o[[email protected]]#./Thanks Hello Worldthank You!

Compiling the program with 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.