Introduction to the GCC for Win32 development environment (4)

Source: Internet
Author: User
Tags automake

Review above:

In the previous article, I mainly introduced the IDE content for you. It mainly includes three ides that I personally prefer. In this article, I will introduce the GCC compilation parameters and the composition and usage of makefile.

GCC for Win32Development Environment(4)

Chapter 3GccRough Exploration --GccAnd additional parameters

Section 1GccFamily member

In general, GCC should be a compiler. But why should I introduce the GCC family members here? In fact, the entire GCC environment is not composed of GCC. It is composed of multiple packages. The interaction of these packages produces the GCC development environment. Some packages are essential for your application development. Without these packages, you will not be able to use GCC normally.

GccBasic Package list.

The basic development environment of GCC consists of several packages. Binutils, which is a tool kit for assisting GCC, includes connectors, compilers, dynamic static library generation programs, and so on. Gcc. This package is GCC itself. Of course, the GCC package also contains several packages, such as core, Java, and Ada. Each package represents a language. Then, it is WIN32API, mingw-runtime, which is a standard function package used in Win32. If you are using cygwin or in a Unix environment, this package is glibc.

So, as described above. The basic GCC packages include:Binutils GCC glibc/[WIN32API, mingw-runtime]With these packages. You can basically start to compile the application.

Of course, if you want to write a small program. Naturally, these packages are enough. But if you want to write a large project. Then, these packages may not be competent for your job. For a large project, there are more than one file to be compiled, and there are dependencies.

Therefore, GCC also includesGmakePackages are used to manage projects. Of course, there is also automake. However, I personally do not like automake. automake helps you automatically manage your projects. Of course, it is quite troublesome to implement automake. Therefore, we use automake to manage small and medium-sized projects, it is better to use gmake to write a script by yourself. However, automake is usually used for source code published applications. If it is used during development, the development cycle will be extended.

Gmake and automake are all compiled programs in batches by compiling scripts. They can determine the source code that needs to be re-compiled automatically and then compile the code based on the given dependency. This can indeed help developers reduce a lot of manpower and development cycles. For example, if you use makefile to manage a project, after you compile the program for the first time, if your source code has not been edited, the next time you call the gmake program, gmake will not compile every file one by one. Instead, you can simply connect to the main program or exit without doing anything (it depends on the makefile script you wrote)

However, for some developers, the above packages still cannot meet their requirements. Because they want to debug the program. Therefore, GCC also includes another package. That isGDB, GDB is developed by GCC for tracking and debugging. It is a command-character debugger. Its functions are quite powerful. Basically, you can do it in VC, or GDB. However, there are still many gdb commands. It is generally enough to master some Basic Debugging commands.

Summary

The GCC development environment includes the following packages.

Binary

Basic Package

Provides basic Assembler and connectors

Gcc

Basic Package

Compilers in various languages, including C, C ++, Ada, and Java

WIN32API, mingwi-runtime/glibc

Basic Package

System function library

Gmake/automake

Package required

Manage programs compiled by the Project

GDB

Additional package

Debug program

Section 2GccCommon compilation Parameters

Different from compilers such as Vc and TC, GCC can easily compile programs at the prompt. GCC compiles programs at the prompt, and there is no lengthy and obscure compilation parameter like VC. On the contrary, there are more flexible and short parameters than VC.

I have to admit that people who do not understand GCC compilation parameters will indeed lose some of the powerful features of GCC. Therefore, I will briefly introduce some basic GCC compilation parameters. Here, I use the C compiler as an example.

Compile binary code

$ Gcc-C yours. C-o yours. o

Using this command, GCC will compile yours. c into the binary code of yours. O. Yours. O is similar to the. OBJ document in VC and TC.

Compile the simplest small program.

$ Gcc-O yours. c

Through this command, GCC will compile yours. C source code into an executable program named yours. Of course, you can also change yours. C to the yours. o file we just introduced. In this way, GCC will use the compiled binary document to link the program. Here, the format features that-O is followed by a list of files. The first parameter is the name of the compiled program file, starting from the second, it is the binary document or source code you need to compile and connect the executable program.

Set your own header file directory as the default header file directory during compilation.

$ Gcc-I "your_include_files_document_path"-C yours. C-o yours. o

The-I parameter in this command will add your_include_files_document_path to your default header file directory. In this way, you can use # include <your_include.h> to import the header file.

Use your own static inventory to store directories during compilation

$ Gcc-L "your_lib_files_document_path"-O yours. o

This command will allow GCC to search for the specified static library in the default lib directory and your_lib_files_document_path during connection.

Use static Connection Library during compilation

$ Gcc-lyour_lib-O yours. o

This command will allow GCC to connect the function you used in libyour_lib.a to the executable program during connection. Note that the static connection library used by GCC is in lib *. A format. During connection, you only need to provide the * content.

Use Optimization during compilation

$ Gcc-O2-C yours. C-o yours. o

Compile the program using optimization methods, including-O2 and-O3-O1. They represent different levels of optimization. The most commonly used is-O2 optimization. Of course, there are also optimizations for special CPUs, which will not be introduced here.

All errors and warnings are displayed during compilation.

$ Gcc-wall-C yours. C-o yours. o

By default, GCC ignores issues such as variable application failure or Initial Value failure. However, if the-wall parameter is used, the editor will list all warning information. In this way, you can know how many errors may occur in your code in other operating systems. (Use this command to see how many places your code is written .)

Add debugging code when compiling the connection

$ Gcc-g-o yours. c

Just as Vc has the debug compiling mode, GCC also has the debug mode. The executable program compiled with the-G parameter is slightly larger than the normal program, and some debugging code is added. The code will be supported by GDB.

Reduce code size during connection

$ Gcc-S-O yours. o

This parameter does not seem to have been seen in UNIX. I do not know what the specific function is. Some people say that the code generated by visual-mingw is small, so they studied her compilation parameters and found that the release mode compilation parameter adds this item. It seems that the compiled code is indeed much reduced.

Get help

$ GCC -- Help

This command indicates the GCC help information. If you have any special requirements, this command may be helpful.

Section 3 how to write a simpleMakefile

After talking about the makefile management project for half a day, I want to talk about how to write it now. In fact, makefile files are relatively easy to write, basically as long as you use the command line, you can write makefile. Next I will briefly introduce the composition and writing of makefile.

One outputHelloworldSimpleMakefile

ALL:

Echo helloworld

This makefile code will print a helloworld on the screen after running. All is similar to the main function in C code. The gmake program will first run the code here when running the makefile code. Note: The ECHO is preceded by <tab>. GCC is very sensitive to spaces.

Add a dependencyMakefile

ALL: depend

@ Echo I am the main

Depend: closeecho

@ Echo I am the depend

Closeecho:

@ Echo off

This makefile code outputs sentences. The difference is that she adds dependencies between several command blocks. All depends on depend, and depend depends on closeecho. In this way, the program will determine the order of file compilation based on the script dependency during compilation.

RunMakefile

$ Make-F makefile

Normally, the-F parameter is not used. The make program searches for a file named makefile in the current directory as the file to be executed. Use-F to specify the name of the makefile.

A completeMakefile

ALL: yours

@ Echo OK

Yours: yours. o

Gcc-O yours. o

Yours. O: yours. c

Gcc-C yours. C-o yours. o

For more information about makefile, see relevant documents.

VOICEOVER:

Indeed, I found out. My articles in this series are not as fast as I expected and promised. It makes me feel helpless. Fortunately, you can understand me. In general, this article is a brief introduction to some of the basics of GCC. I found that many readers are not very familiar with GCC.

It was quite helpless. Recently, I found that there was a villain around me. The name of this person is also hidden. Since last year, I have made rumors in front of some girls who have been chatting with me. Therefore, the article that should have been published in September was delayed until the end of September. Speaking of this person, I hope you will learn from this person. Not like him.

This person claimed to be very good at hardware, but as far as I know, he is nothing more than carrying out the motherboard and hardware numbers of various models. Even basic thread processes are unclear. It is also said that 32bit machines are obsolete machines. I have to admit that this shameless guy is indeed very good at hardware. However, I still think that people who do technology should not pursue superficial things, do not pursue things that are too advanced, and pay more attention to their own conduct and morality.

It may be because I am too old to complain. I hope you do not recommend this. However, once and for all, I cannot recover it.

Finally, I would like to thank you for your support and those who wish to work for me. If you have any questions or suggestions, you can submit them. For timely and accurate replies, please go to http://blog.csdn.net/visioncatto send your message. Thank you.

Studio software development group (SDT)

Studio development team

Dipper (Huang yaokui)

Index:

GCC, windows, compilation parameters, makefile, usage, Structure

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.