Using the GNU tool-GCC compiler under windows

Source: Internet
Author: User

2018-01-12 19:10:42

using the GNU compiler-GCC compiler under Windows

1, MinGW (minimalist GNU on Windows) Compilation tool Introduction

MinGW is a compilation environment that uses only free software to generate pure Win32 executables , which is the minimalist (simplified) GNU on Windows merchants.

Windows ported version of GCC on unix-like system: MinGW compilation System/Tools

In fact, MinGW is not a simple C + + compiler, but a set of GNU tools. In addition to GCC, MinGW also contains some other GNU program development tools (such as gawk bison, etc.). The MinGW was developed to provide a GNU-compliant GNU working environment for those who do not like to work on the Linux (FreeBSD) operating system and remain in Windows. So, using MinGW, we can use the GNU Program development tool just like under Linux.

GCC is the core of MinGW, and GCC is a set of compiler systems that support a wide range of computer programming languages, and are closest to the standard in the implementation of language standards. And GCC can be ported to almost all available computer platforms today. (My computer is also equipped with Devkitpro, which contains GCC's ARM (for GBA/DS/GP32) and MIPS (for PSP) versions. )

GCC itself does not have an IDE interface like VC (the IDE that supports the MinGW compiler, such as Dev C + +, is also available on Windows). source code Editing you can choose any text editor you like (it is said that Microsoft developers, including VC development are not using the VC IDE Editor, but the GNU VIM editor). Then use tools such as make to compile, link, package, and even publish software projects. And like the CVS (SVN) Source version Control tool, people in any corner of the world can get involved in a software project.

Text Editor generates source file +GCC compile system compile (generate executable file?? )

2. MinGW Main Documents

Gcc-core C language Compiler
Gcc-c++ C + + language compiler
Mingw-runtime MinGW Run-time library (this must be installed)
Win32API Win32 API function header file and connection library
Binutils MinGW-based tools such as assemblers and connectors
Debugging tools in GdB MinGW
Mingw32-make make tool for better management of your project

3. GCC Common commands

Gcc-v // View gcc version information gcc-o test   test.c    // execute test.c, the resulting file is named Test, if omitted the default is AGCC-E // pre-processing  //g executable contains debug information  //  -O output_filename, determine the output file name is Output_filename  //-c only compile                         not linked  //

$: Indicates the directory in which the program resides, such as:f:\mingw>

3.1, execute the following command:

$ gcc-o Hello hello.c
Run as follows: $ hello
Output: hello,world!

3.2, we can also compile the following steps:

Step temporarily omitted, too lazy, look at the above code

3.3, we can compile a few files together to generate the same executable file.

For example: A project has MAIN.C, FOO.C, DEF.C to generate Foo executable files.
The compile command is as follows:

$ gcc–c main.c foo.c def.c–o foo // or:$ gcc–o foo main.c foo.c DEF.C

4, library dependencies (library relies on Windows should be the same as under Linux)

A function library is a collection of header files (. h) and library files (. So or. a). Most functions under Linux put the header file in the/usr/include/directory by default, and the library file is placed in the/usr/lib/directory, but not absolutely. Therefore, GCC has a compile option switch that adds header files and library files.
1. Add header file:-I.
For example, in the/home/work/include/directory to compile foo.c required header file Def.h, in order for GCC to find them, you need to use the-I option:

$ gcc foo.c-i/home/work/include/def.h-o foo

2. Add library file:-l
For example, in the/home/work/lib/directory there are links to the required library file libdef.so, in order for GCC to find them, you need to use the-l option:

$ gcc foo.c–l/home/work/lib–ldef.a–o foo

Description: The-l option instructs GCC to connect to the library file libdef.so. The library file name under Linux has a convention, that is, the library file starts with Lib three letters, because all library files follow this convention, so you can omit the Lib three letters when you specify the linked library file name with the-l option.
[Foreign Language]
Library files under Linux are classified as dynamic-link libraries (. so files) and static-link libraries (. a files). GCC defaults to dynamic libraries, and if you want to link static libraries when both dynamic and static libraries are present, you need to specify the-static option. For example, if there is a LIBDEF.A and you want to link LIBDEF.A, the command is as follows:

$ gcc foo.c–l/home/work/lib–static –ldef.a–o foo
5. Code optimization

GCC provides different levels of code optimization. The switch option is:-O n,n with a value of 0 to 3. The default is 1. -o0 indicates no optimizations, while-o3 is the highest optimization. The higher the optimization level, the faster the code runs, but not all of the code can load the highest optimization, and it should depend on the situation. However, the-O2 option is generally used because it provides an ideal balance between optimization length, compile time, and code size.
The following paragraph is my excerpt from someone else's article, said more detailed:
Use the option-O at compile time to tell GCC to reduce both the length and execution time of the code, which is equivalent to-o1. The types of optimizations that can be made at this level depend on the target processor, but generally include both thread jumps and deferred fallback (Deferred stack Pops) optimizations. Option-o2 tells GCC that in addition to completing all-O1 level optimizations, there are additional tuning tasks such as processor instruction scheduling. Option-o3 also includes loop unwinding and other optimizations related to processor characteristics in addition to completing all-O2 level optimizations. Generally speaking, the higher the number, the higher the level of optimization, and it means that the program will run faster.
The following is a concrete example of the GCC code optimization function, the program is shown in Listing 3.

6.15 commonly used GCC commands

http://blog.csdn.net/typename/article/details/8170213

Using the GNU tool-GCC compiler under windows

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.