[Linux] GNU CC

Source: Internet
Author: User

I. GCC usage example

Program Hello. c

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

$ GCC hello. C-O hello // tell GCC to compile/link the source program and use-O to specify the executable program named hell. O.

$./Hello // execute this program

Hello,Linux programming world!

Process details

1. GCC first runs the pre-processing program CPP to expand the macro in hello. C and insert the content contained in the # include file.

2. Compile the pre-processed source code into the target code.

3. Finally, the linking program creates a binary file named "hello ".

Manually recreate these steps:

1, $ gcc-e hello. C-O hello. cpp //-E tells GCC to stop the compilation process after preprocessing.

Now you open the pre-processed hello. cpp file and you will see the expanded macro and insert the content after # include <stdio. h>.

2. GCC-x CPP-output-C hello. cpp-O hello. O // compile hello. cpp as the target code.

-X indicates that GCC starts compilation from the specified step. Here, it starts compilation after preprocessing.

3. GCC hello. O-O hello // link the target file to generate binary code

2. Compilation of C Programs composed of multiple source code files

Program helper. c

#include <stdio.h>void  msg(){   printf("This message sent  from tianshuai\n");}

Program helper. h

void  msg();

Modified hello. c

#include <stdio.h>#include "helper.h"int main(void){    printf("Hello,Linux programming world!\n");    msg();    return 0;}

$ GCC hello. c helper. C-O hello // Compile two files at the same time

Iii. Use of makefile
If the program contains multiple source files, it is difficult to compile the command. Therefore, makefile and make commands are used.

First, make sure that three files hello. c helper. c helper. h have been created.

Create a MAKEFILE file in the directory of the preceding three files.

Hello: Hello. O helper. O helper. h // default target body. To compile the object "hello", there are three dependent bodies: GCC hello. O helper. o-O hello // call the GCC command for make to create hellohelper. o: helper. c helper. h // how to generate a single file gcc-C helper. chello. o: Hello. CGCC-C hello. chello: Hello. c helper. C // here hello depends on two files hello. c helper. CGCC hello. c helper. c-o helloall: Hello clean: Rm hello *. O

Note:

$ Make // compile it into a hello program

If you only want to generate hello. O helper. o

$ Make hello. O helper. o

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.