Mingw installation and usage

Source: Internet
Author: User

The installation method is as follows:

Step one: here to download mingw, slow network speed may not open, is a foreign site (http://sourceforge.net/projects/mingw ).

Step two: double-click the installation package and set everything by default. You can do it after "Next.

Step three: the environment variables may be automatically set during the installation process. Enter GCC in cmd to test the environment variables. If GCC: no input file is displayed, the installation is successful. Otherwise, manually add the C:/mingw/bin directory to the environment variable. The method is as follows (very simple, no need to be afraid)

1. Right-click "my computer" ==> property ==> "advanced" ==> "environment variable"

2. In the user variable, if there is a path variable, select "edit", add a semicolon after the original value, and add the bin directory of mingw. If there is no path, create a new one and add the bin directory of mingw.

3. the test method is the same as above, over.

Here is the English usage. For more information, see my translation. Here is my translation for your reference only. If the translation is inappropriate, please leave it blank.

Use mingw for compilation and creation (due Program)

How to Create a terminal application

Here is an example. The following is an example of a simple C language program code. cut and paste it into a file named hello. C and try again:

# Include <stdio. h>

Int main (INT argc, char ** argv)

{

Printf ("hellon ");

Return (0 );

}

Assume that you want to generate an executable file hello.exe in the release mode from hello.c. Try the following method:

Gcc-C hello. c

This command compiles hello. c into a target file hello. O, and then uses the following method:

Gcc-O hello. o

This command generates an executable hello.exe file from the hello.ofile. As a matter of fact, you can use the following command to compile and link it in one step:

Gcc-O hello. c

The following is a simple code example of a C ++ program. copy and paste the sample code to a file named hello. cpp and try again:

# Include <iostream>

Int main (INT argc, char ** argv)

{

STD: cout <"hello" <STD: Endl;

Return (0 );

}

For C ++ programs, use the following command to compile and link:

G ++-C hello. cpp

G ++-O hello. o

(You can also merge them in one step)

2. How to Create a Windows Application

Here is an example. The following is a simple example of the code for a Windows application. copy and paste it into a file named hello. C and try again:

# Include <windows. h>

Int winapi winmain (hinstance,

Hinstance hprevinstance,

Pstr szcmdline,

Int icmdshow)

{

MessageBox (null, "hello", "Hello Demo", mb_ OK );

Return (0 );

}

If you want to create a Windows executable‑hello.exe file from a C language source file named hello. C, try the following command:

Gcc-C hello. c

First, the target file hello. O is generated, and then the following code is used:

Gcc-O hello. O-mwindows

This will create an executable hello.exe from hello.o,-mwindows is used to indicate that the windows application is to be created rather than the Program executed from the terminal, it ensures that the correct Windows library is linked.

To get a terminal screen and a standard Windows application at the same time

-Added a-mconsole flag after the mwindows flag)

Assume that you have new resources (a suffix is. RC files) must be added to your program. You need to compile these resource files and other resource files, and include these compiled resource files in the link to create your program. The following example shows how to compile and link a file called resfile. RC resource file:

Windres-O resfile. O resfile. RC

Gcc-O hello. O resfile. O-mwindows

3. How to Create a DLL file?

Here is an example. Copy and paste the following code into a file named dllfct. h:

# Ifdef build_dll

// The DLL exports

# Define export _ declspec (dllexport)

# Else

// The EXE imports

# Define export _ declspec (dllimport)

# Endif

// Function to be imported/exported

Export void tstfunc (void );

Copy and paste the following code into a file named dllfct. C:

# Include <stdio. h>

# Include "dllfct. H"

Export void tstfunc (void)

{

Printf ("hellon ");

}

Copy and paste the following code into a file named hello. C:

# Include "dllfct. H"

Int main ()

{

Tstfunc ();

Return (0 );

}

To create a DLL and a program using it, try the following command:

Gcc-C hello. c

Gcc-C-dbuild_dll dllfct. c gcc-shared-o tst. dll-wl, -- Out-implib, libtstdll. A dllfct. o

Gcc-O hello.exe hello. O-L./-ltstdll

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.