Use makefile in windows

Source: Internet
Author: User
  1. 1.Preparations: 

Program:

MinGW-3.1.0-1.exe windows gcc, C language compilation tool

: Http://umn.dl.sourceforge.net/sourceforge/mingw/MinGW-3.1.0-1.exe

Tools used by make to compile programs according to makefile rules

Location: window/system32. If not, find it at http://www.ming#org/download.shtml.

Configuration environment:

Environment Variable Configuration:

Control Panel> system> advanced> environment variables> system variables> Add the bin directory under the MinGW installation directory to the path, or run the command line: set PATH = bin directory under the MinGW installation directory

Use C: \ WINDOWS \ system32to upload the file to pathto find make.exe.

 

Create four files:

// --------------------------- Main. c :---------------------------//

# Include "stdio. h"

Main ()

{

Func ();

Printf ("this is main \ n ");

Getch ();

}

 

// --------------------------- Func. c :---------------------------//

# Include "stdio. h"

Func ()

{

Printf ("this is func \ n ");

Getch ();

}

 

An empty file named makefile or Makefile is required because make searches for the file by default.

 

Create a compile. bat file

Content:

Make

Cmd

 

 

  1. 2.WriteMakefile

Example:

Test: main. o func. o

Gcc-o test main. o func. o

Func. o: func. c

Gcc-c func. c

Main. o: main. c

Gcc-c main. c

 

Line 1: generate the test executable file, which depends on main. o func. o. That is to say, generate the test file,

Line 2: the front side of gcc must be a tab or seven spaces, indicating that the test is compiled and generated.

The following is the dependency generation rule.

 

Then run compile.batto generate the test.exe file, and append the files that generate the func. o and main and o intermediate code, similar to the obj file in VC.

 

  1. 3.MakefileWrite rules 

See: http://www.stlchina.org/twiki/bin/view.pl/ScriptProgram/LearnMakefile

  1. 4.GccParameters 

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.

 

From above: http://blog.csdn.net/zhanghan3/archive/2006/10/14/1334308.aspx

 

Note the following:

1: The above link address is not available, the latest mingw: http://sourceforge.net/projects/mingw/files/Automated%20MinGW%20Installer/

 

2: After installation, there will be no make.exefile in mingw \ bin, and change mingw32-make.exe to make.exe.

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.