Compile and run C notes on Ubuntu

Source: Internet
Author: User
As a beginner, I enjoy and care about every bit of knowledge I have learned. I wrote this entry-level article mainly to record my own little bit/s, leave a clue when I look for memories for myself 10 years later. It would be best if I could help some new Linux learners! In Ubuntu, IDE development is not required. If you compile and debug the program under GNU, you can only enter commands. Although it is cumbersome, it is very efficient, in addition, it looks cool to enter commands on the terminal. GNU is the recursive mechanism of gun' snotunix

As a beginner, I enjoy and care about every bit of knowledge I have learned. I wrote this entry-level article mainly to record my own little bit/s, leave a clue when I look for memories for myself 10 years later. It would be best if I could help some new Linux learners!

In Ubuntu, IDE development is not required. If you compile and debug the program under GNU, you can only enter commands. Although it is cumbersome, it is very efficient, in addition, it looks cool to enter commands on the terminal.

GNU is written recursively by GUN's not unix. GNU was originally initiated by Daniel to develop a free operating system and a free software movement. Gcc is the gnu c language compiler. Later, it was developed to compile C ++ and became the abbreviation of GNU compiler collection. This is a simple history and background, because we use gcc to compile our c program.

During compilation, you must first enter the directory where your c file is located. Otherwise, you cannot find the original file. You can run the ls command to view the files in the current directory.

I. Compile a single C file

Gcc text. c generates the. out file by default, that is, the default target code file.

Gcc-c text. c will generate the target file with the same name as the source file: text. o

Gcc text. c-o liu generates an executable file named liu or gcc-o liu text. c./liu to execute the program.


Ii. Use non-system default Class Libraries

For example, the multithreading class library pthread is used.

Gcc text. c-o liu-lpthread or gcc-o liu text. c-lpthread must be placed at the end, that is, add the class library you want to use after-l.

3. Compile multiple source files, that is, compile makefile or Makefile.


The advantage of makefile is to prevent source files from being repeatedly compiled.

For example, there are many source files, one of which can be modified one day. If you do not write makefile, you need to re-compile all the files in the project. This is very troublesome. With makefile, describes the dependency between each source file. When a source file changes, you only need to recompile the associated source file.

For example, the following files are available:

One. h

One. c

Two. h

Two. c

Main. c

The one. h and two. h header files are introduced in main. c, and one. c two. c implements the functions defined in the two header files respectively.

There are two ways to compile and run the program,

I. manual compilation

Gcc-c one. c

Gcc-c two. c

Gcc-c main. c

Gcc-o main. o one. o two. o

./Main

Ii. makefile


Write the following command in any text editor and save it as makefile.

Main: main. o one. o two. o

Tab key gcc (or cc)-o main. o one. o two. o

Main. o: main. c one. h two. h

Tab key gcc (or cc)-c main. c


One. o: one. c one. h

Tab key gcc (or cc)-c one. c

Two. o: two. c two. h

Tab key gcc (or cc)-c two. c

These source files must be in the same directory and in main. introduce one in c. h and two. use "one. h "" two. h "do not use <> otherwise, the compiler will go to the system class library file and cannot find it.

Then input the make command in the terminal of the same directory, and the compilation starts. If there is no error, execute./main to run your program.

You can also streamline makefile:

There are three symbols:

1 $ @ indicates the target file

2 $ <indicates the first file

3 $ ^ indicates all dependent files

Therefore, the preceding command can be simplified:

Main: main. o one. o two. o

Tab key gcc (or cc)-o $ @ $ ^


Main. o: main. c one. h two. h

Tab key gcc (or cc)-c $ <


One. o: one. c one. h

Tab key gcc (or cc)-c $ <


Two. o: two. c two. h

Tab key gcc (or cc)-c $ <

The makefile can also be simplified:

A new symbol:. c. o

This symbol automatically searches for the associated source file and header file

Therefore, makefile can also be written as follows:


Main: main. o one. o two. o

Tab key gcc (or cc)-o $ @ $ ^

. C. o:
Tab key gcc (or cc) $ <

It is best to add the clean command to delete the generated target file and execution file. It complies with the elegant and concise principle to facilitate re-compilation.

Clean:

AB key gcc (or cc) rm main. o one. o two. o


Remember the format of makefile. As for the question, you can only say that this is a standard. makefile has many advanced usage and will be learned later.

OK. If you have any questions, please leave a message.

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.