Mingw w64 как установить

от admin

Using GCC with MinGW

In this tutorial, you configure Visual Studio Code to use the GCC C++ compiler (g++) and GDB debugger from mingw-w64 to create programs that run on Windows.

After configuring VS Code, you will compile and debug a simple Hello World program in VS Code. This tutorial does not teach you about GCC, GDB, Mingw-w64, or the C++ language. For those subjects, there are many good resources available on the Web.

If you have any problems, feel free to file an issue for this tutorial in the VS Code documentation repository.

Prerequisites

To successfully complete this tutorial, you must do the following steps:

Install the C/C++ extension for VS Code. You can install the C/C++ extension by searching for ‘c++’ in the Extensions view ( ⇧⌘X (Windows, Linux Ctrl+Shift+X ) ).

Get the latest version of Mingw-w64 via MSYS2, which provides up-to-date native builds of GCC, Mingw-w64, and other helpful C++ tools and libraries. You can download the latest installer from the MSYS2 page or use this link to the installer.

Follow the Installation instructions on the MSYS2 website to install Mingw-w64. Take care to run each required Start menu and pacman command.

Install the Mingw-w64 toolchain ( pacman -S —needed base-devel mingw-w64-x86_64-toolchain ). Run the pacman command in a MSYS2 terminal. Accept the default to install all the members in the toolchain group.

Add the path to your Mingw-w64 bin folder to the Windows PATH environment variable by using the following steps:

  1. In the Windows search bar, type ‘settings’ to open your Windows Settings.
  2. Search for Edit environment variables for your account.
  3. Choose the Path variable in your User variables and then select Edit.
  4. Select New and add the Mingw-w64 destination folder path to the system path. The exact path depends on which version of Mingw-w64 you have installed and where you installed it. If you used the settings above to install Mingw-w64, then add this to the path: C:\msys64\mingw64\bin .
  5. Select OK to save the updated PATH. You will need to reopen any console windows for the new PATH location to be available.

Check your MinGW installation

To check that your Mingw-w64 tools are correctly installed and available, open a new Command Prompt and type:

  1. If you don’t see the expected output or g++ or gdb is not a recognized command, make sure your PATH entry matches the Mingw-w64 binary location where the compilers are located. If the compilers do not exist at that PATH entry, make sure you followed the instructions on the MSYS2 website to install Mingw-w64.
  2. If gcc has the correct output but not gdb , then you need to install the packages you are missing from the Mingw-w64 toolset.
    • Missing the mingw-w64-gdb package is one cause of the "The value of miDebuggerPath is invalid." message upon attempted compilation if your PATH is correct.

Create Hello World

From a Windows command prompt, create an empty folder called projects where you can place all your VS Code projects. Then create a sub-folder called helloworld , navigate into it, and open VS Code in that folder by entering the following commands:

The "code ." command opens VS Code in the current working folder, which becomes your "workspace". Accept the Workspace Trust dialog by selecting Yes, I trust the authors since this is a folder you created.

As you go through the tutorial, you will see three files created in a .vscode folder in the workspace:

  • tasks.json (build instructions)
  • launch.json (debugger settings)
  • c_cpp_properties.json (compiler path and IntelliSense settings)

Add a source code file

In the File Explorer title bar, select the New File button and name the file helloworld.cpp .

Add hello world source code

Now paste in this source code:

Now press ⌘S (Windows, Linux Ctrl+S ) to save the file. Notice how the file you just added appears in the File Explorer view ( ⇧⌘E (Windows, Linux Ctrl+Shift+E ) ) in the side bar of VS Code:

File Explorer

You can also enable Auto Save to automatically save your file changes, by checking Auto Save in the main File menu.

The Activity Bar on the far left lets you open different views such as Search, Source Control, and Run. You’ll look at the Run view later in this tutorial. You can find out more about the other views in the VS Code User Interface documentation.

Note: When you save or open a C++ file, you may see a notification from the C/C++ extension about the availability of an Insiders version, which lets you test new features and fixes. You can ignore this notification by selecting the X (Clear Notification).

Explore IntelliSense

In your new helloworld.cpp file, hover over vector or string to see type information. After the declaration of the msg variable, start typing msg. as you would when calling a member function. You should immediately see a completion list that shows all the member functions, and a window that shows the type information for the msg object:

Statement completion IntelliSense

You can press the Tab key to insert the selected member; then, when you add the opening parenthesis, you will see information about any arguments that the function requires.

Run helloworld.cpp

Remember, the C++ extension uses the C++ compiler you have installed on your machine to build your program. Make sure you have a C++ compiler installed before attempting to run and debug helloworld.cpp in VS Code.

Open helloworld.cpp so that it is the active file.

Press the play button in the top right corner of the editor.

Choose C/C++: g++.exe build and debug active file from the list of detected compilers on your system.

You’ll only be asked to choose a compiler the first time you run helloworld.cpp . This compiler will be set as the "default" compiler in tasks.json file.

After the build succeeds, your program’s output will appear in the integrated Terminal.

The first time you run your program, the C++ extension creates tasks.json , which you’ll find in your project’s .vscode folder. tasks.json stores build configurations.

Your new tasks.json file should look similar to the JSON below:

Note: You can learn more about tasks.json variables in the variables reference.

The command setting specifies the program to run; in this case that is g++. The args array specifies the command-line arguments that will be passed to g++. These arguments must be specified in the order expected by the compiler.

This task tells g++ to take the active file ( $ ), compile it, and create an executable file in the current directory ( $ ) with the same name as the active file but with the .exe extension ( $.exe ), resulting in helloworld.exe for our example.

The label value is what you will see in the tasks list; you can name this whatever you like.

The detail value is what you will as the description of the task in the tasks list. It’s highly recommended to rename this value to differentiate it from similar tasks.

From now on, the play button will read from tasks.json to figure out how to build and run your program. You can define multiple build tasks in tasks.json , and whichever task is marked as the default will be used by the play button. In case you need to change the default compiler, you can run Tasks: Configure default build task. Alternatively you can modify the tasks.json file and remove the default by replacing this segment:

Modifying tasks.json

You can modify your tasks.json to build multiple C++ files by using an argument like "$/*.cpp" instead of $ .This will build all .cpp files in your current folder. You can also modify the output filename by replacing "$\\$.exe" with a hard-coded filename (for example "$\\myProgram.exe" ).

Debug helloworld.cpp

  1. Go back to helloworld.cpp so that it is the active file.
  2. Set a breakpoint by clicking on the editor margin or using F9 on the current line.
  3. From the drop-down next to the play button, select Debug C/C++ File.
  4. Choose C/C++: g++ build and debug active file from the list of detected compilers on your system (you’ll only be asked to choose a compiler the first time you run/debug helloworld.cpp ).

The play button has two modes: Run C/C++ File and Debug C/C++ File. It will default to the last-used mode. If you see the debug icon in the play button, you can just click the play button to debug, instead of using the drop-down.

Explore the debugger

Before you start stepping through the code, let’s take a moment to notice several changes in the user interface:

The Integrated Terminal appears at the bottom of the source code editor. In the Debug Output tab, you see output that indicates the debugger is up and running.

The editor highlights the line where you set a breakpoint before starting the debugger:

The Run and Debug view on the left shows debugging information. You’ll see an example later in the tutorial.

At the top of the code editor, a debugging control panel appears. You can move this around the screen by grabbing the dots on the left side.

Debugging controls

Step through the code

Now you’re ready to start stepping through the code.

Click or press the Step over icon in the debugging control panel.

This will advance program execution to the first line of the for loop, and skip over all the internal function calls within the vector and string classes that are invoked when the msg variable is created and initialized. Notice the change in the Variables window on the left.

Debugging windows

In this case, the errors are expected because, although the variable names for the loop are now visible to the debugger, the statement has not executed yet, so there is nothing to read at this point. The contents of msg are visible, however, because that statement has completed.

Press Step over again to advance to the next statement in this program (skipping over all the internal code that is executed to initialize the loop). Now, the Variables window shows information about the loop variables.

Press Step over again to execute the cout statement. (Note that as of the March 2019 release, the C++ extension does not print any output to the Debug Console until the loop exits.)

If you like, you can keep pressing Step over until all the words in the vector have been printed to the console. But if you are curious, try pressing the Step Into button to step through source code in the C++ standard library!

Breakpoint in gcc standard library header

To return to your own code, one way is to keep pressing Step over. Another way is to set a breakpoint in your code by switching to the helloworld.cpp tab in the code editor, putting the insertion point somewhere on the cout statement inside the loop, and pressing F9 . A red dot appears in the gutter on the left to indicate that a breakpoint has been set on this line.

Breakpoint in main

Then press F5 to start execution from the current line in the standard library header. Execution will break on cout . If you like, you can press F9 again to toggle off the breakpoint.

When the loop has completed, you can see the output in the Integrated Terminal, along with some other diagnostic information that is output by GDB.

Debug output in terminal

Set a watch

Sometimes you might want to keep track of the value of a variable as your program executes. You can do this by setting a watch on the variable.

Place the insertion point inside the loop. In the Watch window, click the plus sign and in the text box, type word , which is the name of the loop variable. Now view the Watch window as you step through the loop.

Watch window

Add another watch by adding this statement before the loop: int i = 0; . Then, inside the loop, add this statement: ++i; . Now add a watch for i as you did in the previous step.

To quickly view the value of any variable while execution is paused on a breakpoint, you can hover over it with the mouse pointer.

Mouse hover

Customize debugging with launch.json

When you debug with the play button or F5 , the C++ extension creates a dynamic debug configuration on the fly.

There are cases where you’d want to customize your debug configuration, such as specifying arguments to pass to the program at runtime. You can define custom debug configurations in a launch.json file.

To create launch.json , choose Add Debug Configuration from the play button drop-down menu.

Читать:
E book reader что это такое

You’ll then see a dropdown for various predefined debugging configurations. Choose C/C++: g++.exe build and debug active file.

VS Code creates a launch.json file, which looks something like this:

In the JSON above, program specifies the program you want to debug. Here it is set to the active file folder ( $ ) and active filename with the .exe extension ( $.exe ), which if helloworld.cpp is the active file will be helloworld.exe . The args property is an array of arguments to pass to the program at runtime.

By default, the C++ extension won’t add any breakpoints to your source code and the stopAtEntry value is set to false .

Change the stopAtEntry value to true to cause the debugger to stop on the main method when you start debugging.

From now on, the play button and F5 will read from your launch.json file when launching your program for debugging.

C/C++ configurations

If you want more control over the C/C++ extension, you can create a c_cpp_properties.json file, which will allow you to change settings such as the path to the compiler, include paths, C++ standard (default is C++17), and more.

You can view the C/C++ configuration UI by running the command C/C++: Edit Configurations (UI) from the Command Palette ( ⇧⌘P (Windows, Linux Ctrl+Shift+P ) ).

Command Palette

This opens the C/C++ Configurations page. When you make changes here, VS Code writes them to a file called c_cpp_properties.json in the .vscode folder.

Here, we’ve changed the Configuration name to GCC, set the Compiler path dropdown to the g++ compiler, and the IntelliSense mode to match the compiler (gcc-x64).

Command Palette

Visual Studio Code places these settings in .vscode\c_cpp_properties.json . If you open that file directly, it should look something like this:

You only need to add to the Include path array setting if your program includes header files that are not in your workspace or in the standard library path.

Compiler path

The extension uses the compilerPath setting to infer the path to the C++ standard library header files. When the extension knows where to find those files, it can provide features like smart completions and Go to Definition navigation.

The C/C++ extension attempts to populate compilerPath with the default compiler location based on what it finds on your system. The extension looks in several common compiler locations.

The compilerPath search order is:

  • First check for the Microsoft Visual C++ compiler
  • Then look for g++ on Windows Subsystem for Linux (WSL)
  • Then g++ for Mingw-w64.

If you have Visual Studio or WSL installed, you may need to change compilerPath to match the preferred compiler for your project. For example, if you installed Mingw-w64 version 8.1.0 using the i686 architecture, Win32 threading, and sjlj exception handling install options, the path would look like this: C:\Program Files (x86)\mingw-w64\i686-8.1.0-win32-sjlj-rt_v6-rev0\mingw64\bin\g++.exe .

Troubleshooting

MSYS2 is installed, but g++ and gdb are still not found

You must follow the steps on the MSYS2 website and use the MSYS CLI to install Mingw-w64, which contains those tools. You will also need to install the full Mingw-w64 toolchain ( pacman -S —needed base-devel mingw-w64-x86_64-toolchain ) to get the gdb debugger.

MinGW 32-bit

If you need a 32-bit version of the MinGW toolset, consult the Downloading section on the MSYS2 wiki. It includes links to both 32-bit and 64-bit installation options.

Install the Mingw and the Cygwin C/C++ compilers under windows

Two options for compiling C and C++ , and other kinds of source code files under windows , are : the cygwin-gcc compiler , and the mingw-gcc compiler .

For source code , that uses the posix api , the cygwin-gcc compiler can be used , it provides a substantial implementation of the posix api , provided by the cygwin1.dll library .

cygwin-gcc , can also be used to compile source code , which uses the windows api . In both cases , the cygwin1.dll library , must be made available to the executing program .

For source code , that only uses the windows api , the mingw-gcc compiler can be used . The produced executable , does not depended on cygwin1.dll , it just depends on windows libraries .

To install these compilers , two unix like environment , which provide unix commands in the form of windows executables , are available , the first one is the Cygwin environment , and the second one is MSYS2 environment .

Both environment , provide both compilers , the MSYS2 environment provides a package manager , pacman , that allows the installation of packages , whereas Cygwin does not provide a package manager , package installation is done through the installer , each time a package is to be installed or removed .

MSYS2

Installation

To install MSYS2 , download the installer from the MSYS2 website , and double click it .

Click next , on the first screen , next for the install folder , next for the start menu shortcuts , after that msys2 will start installing .

General Usage Instructions

The prefix i686=32-bit x86_64=64-bit shows what kind of binaries the toolchain builds. The suffix shows what OS the toolchain is built for. If you need to build 32-bit binaries on Windows, it is recommended to use the i686. win32 package.

    : high quality toolchains, providing dual-target builds(host=x32 -> target=x32/x64 & host=x64 -> target=x64/x32). GCC 4.6.2 and up is covered. The builds are divided into two categories: stable and unstable. Also, the builds provide two models of threads: posix/win32. In addition to this, you can select dwarf/sjlj/seh builds at your choice. And one more important feature of the builds is that the builds include python interpreter built within the MinGW-builds project, and which is used for the ‘GDB pretty-printers’. Also provides other useful packages.

The prefix x32=32-bit x64=64-bit shows what kind of binaries the toolchain builds. If you need to build 32-bit binaries on Windows, it is recommended to use the x32 package.

  • Automated builds: produced regularly, as often as possible. These will use upstream GCC versions and may not be very stable at times, but these are tested to at least function. The binary packages do not include optional mingw-w64 SDKs, including the DXSDK and DDK.

For linux, see below.

Installing the toolchain

Windows native

TDM-GCC comes with an easy installer, please use that. The other builds are quite straightforward to set up:

  • extract the toolchain to a directory: 32-bit mingw-w32: eg C:\mingw32 so that C:\mingw32\bin contains i686-w64-mingw32-gcc.exe 64-bit mingw-w64: eg C:\mingw64 so that C:\mingw64\bin contains x86_64-w64-mingw32-gcc.exe
  • open a cmd.exe and do set PATH=C:\mingw64\bin;%PATH% for 64-bit building. set PATH=C:\mingw32\bin;%PATH% for 32-bit building.
  • You should be ready to go. Execute i686-w64-mingw32-gcc -v or x86_86-w64-mingw32-gcc -v to see that everything has gone well.
  • (Autobuilds only) If you need mingw32-make, please download it from the mingw-w64 downloads site under External binary packages.

Linux

Several distributions already provide mingw64 packages:

  • Arch (AUR): https://aur.archlinux.org/packages.php?ID=53926
  • Debian: http://packages.debian.org/sid/mingw-w64 and related packages.
  • Fedora: https://fedoraproject.org/wiki/MinGW/Tutorial
  • Ubuntu: http://packages.ubuntu.com/source/lucid/mingw-w64 and related packages

Using the Toolchain

The toolchain consists of compilers, linkers assemblers and supporting utilities (comparable to what you get with the Windows SDK. It does not include an IDE.

If you need to compile GNU software which requires the use of a configure script for Windows, see [MSYS].

CMake can be used for generating «MinGW Makefiles», which of course work fine for mingw-w64/w32.

How to install MinGW64 (non of the approaches work)

I am trying to install MinGW64 on Windows 10. Here is what I tried:

Approach 1

I went to http://mingw-w64.org/doku.php/download/win-builds and from there to http://win-builds.org/doku.php/download_and_installation_from_windows where I downloaded Win-builds 1.5.0. I run the exe and let it install everything. When it gets to install mingw64 it says that the Download failed (no further information). What should I do?

Approach 2

I also downloaded mingw-w64-v7.0.0 from https://sourceforge.net/projects/mingw-w64/, extracted the zip but have not idea what to do with the contents:

Approach 3

In https://stackoverflow.com/a/48519212/4533188 it was said that should download from https://sourceforge.net/projects/mingw-w64/files/mingw-w64/, so I went there downloaded MinGW-W64-install.exe and ran it. The post says further

Once the compressed file downloaded, you have just to extract and copy/paste the MinGW64 folder( with the pre-compiled librairies) to your chosen folder ( in my case : C:\mingw64)

but I got a lot of files installed to C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\ . So I copied the files of the folder to C:\MinGW64 , but it seems I still do not have it installed.

Approach 4

I went to https://www.msys2.org/, downloaded the x86_64 version, went through the steps outlined in https://stackoverflow.com/a/40800825/4533188 — except for point 7. which I substituted with running pacman -S mingw-w64-x86_64-cmake . This seems to have worked, but the folder C:\MinGW64 is empty and running gcc.exe —version says, the command could not be found. I have no idea where msys2 installed what and how I have to link/move what to get gcc to run.

5 Answers 5

The following approach did work for me. I got there via the link in https://stackoverflow.com/a/39113943/4533188

  1. Got to https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/
  2. Click on the newest version (e.g. 8.1.0)
  3. Click on x86_64-posix-sjlj
  4. Download the 7z-Archive

Alternative way of clicking around:

  1. Got to https://sourceforge.net/projects/mingw-w64/
  2. Click on files
  3. Click on x86_64-posix-sjlj of the version you want
  4. Download the 7z-Archive
  1. Unzip the archive
  2. Inside it there is the folder mingw64 — move it (and its contents) into C:\MinGW64
  3. The gcc can be found at C:\MinGW64\mingw64\bin\gcc.exe

Otiel's user avatar

Regarding your approaches:

  • Approach 1 — the win-builds site is out of date and should not be used now.
  • Approach 2 — you downloaded source code, only do this if you are set up to build from source.
  • Approach 3 — it sounds like you installed it correctly and it is unclear why you feel it is not installed. NOTE — this approach goes more smoothly if you choose the option «MinGW-W64-install.exe» instead of unpacking a zip file; this will also install batch files to the start menu which set up the environment for Windows Command Prompt .
  • Approach 4 — again it sounds like you installed it correctly but have a faulty expectation about the install path. MSYS2 packages will go underneath the MSYS2 root directory. And it is not clear where you typed «gcc.exe» (you should just type gcc from the MSYS2 mingw-w64 shell, which will be accessible from start menu and desktop shortcut if you chose that during the MSYS2 install), and you probably do not want to skip step 7 (CMake relies on gmake; you could install cmake in addition to make).

For approach 4, this list of steps is slightly better than the list you linked

I had a similar problem installing MINGW-W64. What I did was to Run as Administrator, make sure the installation path contains no spaces.

Initially, I was installing it at C:/Program Files (x86)/Mingw-w64 so I change to install it directly on C:/Mingw-w64

Jared Forth's user avatar

Method to switch from MinGW-w32 to MinGW-w64

  • Download the executable file of MinGW-w64 Refer the EDIT

(Executable file link may change for future releases, this is for Version 8.1.0, Kindly cross verify the latest version before installing from this link)

  • Installation Process (In Settings):

If anyone is trying to add MinGW-w64 as a PATH variable and is not able to find the gdb.exe in C:\msys64\mingw64\bin , try looking for it in the Program files.

The gdb.exe and other files are located in this directory.

After making some changes in the launch.json file in VSCode, the MinGW-w64 GDB debugger started giving errors because there was 2 versions of MinGW in different folders i.e, MINGW and mingw64!

It is essential that the installation path does not contain any spaces
(i.e., space in "Program Files"), this will create conflicts later.

1. Uninstalled all the versions of GCC that was installed in the PC — Cygwin, MSYS2(32 bit) and mingw64(64 bit) and installed the MinGW-w64 again, this time using the MSYS2.

Please start afresh, if debugger is giving errors and if versions are clashing!

2. Download the MSYS2 installer from this link.

Install process is simple, follow the steps mentioned in the website!

It is essential that the installation path does not contain any spaces. Therefore, we cannot install MinGW-w64 in Program Files.

3. After Installation is complete: Open MSYS2 terminal (pink icon).

Update the package database and base packages using:

After this, Update rest of the base packages using:

4. Now switch over to MSYS2 MinGW 64-bit terminal (blue icon).

To install gcc and g++ for C and C++.

To install debugger (gdb).

5. Now you’re all SET!

6. Finally, remove the old environment variables if any are left and add the new environment variable to the PATH!

BEFORE DEBUGGING FILES IN VSCode, MAKE SURE TO ADD -g tag while building, otherwise breakpoints will be ignored by the debugger!

Footnote: It’s very important to keep all the versions in one folder, if folders are different, then life will get tough later!

Похожие статьи