This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions.
There are 7 references cited in this article, which can be found at the bottom of the page.
This article has been fact-checked, ensuring the accuracy of any cited facts and confirming the authority of its sources.
This article has been viewed 571,123 times.
Learn more...
If you use the commercial version of Microsoft Visual Studio to code, it has a built-in C/C++ compiler that's easy to use. If you're using Visual Studio code or a different editor without a compiler, you can use a port of GCC and G++ called MinGW). This guide assumes that your source code is for a console application and does not require any outside libraries. Read on to learn how to convert C++ code (CPP files) into an executable EXE file.
Quick Steps
- Install MSYS2 for Windows.
- Open an MSYS2 terminal and run the command to install MinGW.
- Add the compiler's path to your user environment variables.
- Use "g++ yourprogram.cpp" at the prompt or run the compiler from VS Code.
Steps
-
Install MSYS2 for Windows. If you're using Visual Studio Code or another development environment that doesn't come with a compiler, you can use the MinGW GCC and G++ compiler to turn CPP files into executables, just like in Linux. To get started, head to https://www.msys2.org/ and click the link next to Download the installer, then double-click the installer to run it.[1]
- When installing, choose a short name for your install folder with no special characters, e.g., C:\msys64.
- You should install MSYS2 on an NTFS volume on the local machine, not on a network drive.[2]
-
Open an MSYS2 terminal. The MSYS2 terminal should open automatically after installation. If it doesn't, open the MSYS2 folder in the Windows menu, then select MSYS2 UCRT64.Advertisement
-
Install MinGW GCC and G++. To do this:
- Type this command into the terminal and press Enter: pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain.
- When you see the list of tools, press Enter to install all of them at once.
- Press Y to proceed with installation.[3]
- Once the installation is complete, MinGW's version of GCC/G++ will be installed and ready to use.
-
Open your user environment variables for editing. Now you'll want to add the compiler's installation path to your Windows environment variables so you can run it from command prompt.
- Right-click the Windows menu and select Settings.
- In the search bar, type environment variables. You should see two search results.
- Click Edit environment variables for your account.
-
Add the path to the MinGW compiler. Here's how:
- Select Path under "User variables" and click the Edit… button.
- Click New.
- Add the path to the ucrt64\\bin\ folder inside the MSYS2 folder you created. For example, if you installed MSYS2 in C:\MSYS2, type C:\MSYS2\ucrt64\bin.
- Click OK
-
Compile the CPP file to EXE in VS Code (optional). If you're using Visual Studio Code, you can now easily compile and run your C++ program from within the editor. Here's how:
- Reopen your CPP file in VS Code.
- Click the Play button at the top-right and select Run C/C++ File.
- From the list of compilers, select C/C++: g++.exe build and debug active file. You'll only have to do this the first time you use the compiler.
- Once selected, your program will be compiled, and you'll see the output in the terminal.
-
Compile from the command prompt. If you just want to compile the CPP file into EXE at the prompt, it's easy:
- Right-click the Start menu and select File Explorer.
- Open the folder that contains your CPP file(s).
- Hold the ⇧ Shift key as you click an empty area in the folder, then select Open Command window here.
- If you're using Windows 11, select Open in Terminal, click the down arrow at the top of the Terminal, and select Command Prompt.
- Type g++ yourprogram.cpp (replace that name with the name of your actual CPP file) and press ↵ Enter to compile the CPP file into an EXE. As long as there are no errors in your C++ code, a new file ending in "EXE" will appear in the current folder.[4]
-
Open Visual Studio on your PC. You'll find it in the Start menu. Use this method if you're using the full IDE commercial version of Visual Studio.
- If you're using a free coding app like Visual Studio Code or want to compile from the command prompt, see the Using MinGW method.
-
Create a new project. If you haven't already done so, click the File menu, select New, and then select Project.[5] A dialog window will appear.
-
Set up your project: The steps are a little different depending on the version you're using:[6]
- Visual Studio 2019: Select C++ from the "Language" menu, Windows from the "Platform" menu, and then Console as the "Project type." Select Console App, click Next, enter a project name, and then click Create.
- Visual Studio 2017: Click Windows Desktop and then Windows Console Application. Type a name for the project and click OK.[7]
-
Open the Solution Explorer if it's not already open. If you don't already see a window with this name, click the View menu and select Solution Explorer to display it now.
-
Add your CPP file(s) to the "Source Files" folder. The folder is in the Solution Explorer. You can drag them there from another window. Rename the main CPP file (the one that contains "int main()") to the name of the project that you chose if it's not already the same.[8]
- If you have any .H files, add them to the "Header Files" directory.
-
Click the Build menu. It's at the top of the window.[9]
-
Click Build Solution on the menu. This compiles your program into the EXE format. Your build results will appear in the "Output" window at the bottom of the workspace.
- To test your program from Visual Studio, click the Debug menu and select Start without debugging.
- To test it from the File Explorer, right-click the name of your app in the Solution Explorer window, select Open Folder in File Explorer, and then double-click the Debug folder—you'll find your app here.
Community Q&A
-
QuestionIs Turbo C+ good for compiling C++ programs?Community AnswerIt is a good compiler, but it's outdated and doesn't support modern C++. I would recommend Visual Studio instead.
-
QuestionCan I run trurboC3 file in visual studio without any problem?Community AnswerYes, you can. Just locate the file in the location of computer and open it with visual studio or you directly access it from visual studio just search the file and run it.
-
QuestionHow do I rename the executable file from .exe to any other name?Community AnswerRight Click your project in Solution Explorer. Click on the Properties. Select the application tab. Change the assembly name to whatever new name you want to have of your exe.
Video
Tips
References
- ↑ https://code.visualstudio.com/docs/cpp/config-mingw
- ↑ https://www.msys2.org/
- ↑ https://code.visualstudio.com/docs/cpp/config-mingw
- ↑ https://code.visualstudio.com/docs/cpp/config-mingw
- ↑ https://docs.microsoft.com/en-us/cpp/windows/walkthrough-creating-a-standard-cpp-program-cpp?view=vs-2019
- ↑ https://code.visualstudio.com/docs/languages/cpp
- ↑ https://docs.microsoft.com/en-us/cpp/windows/walkthrough-creating-a-standard-cpp-program-cpp?view=vs-2017
- ↑ https://code.visualstudio.com/docs/languages/cpp
- ↑ https://docs.microsoft.com/en-us/cpp/get-started/tutorial-console-cpp?view=vs-2019
About This Article
1. Open Visual Studio.
2. Set up your project.
3. Add your CPP code to the "Source Files" folder.
4. Click the Build menu.
5. Click Build Solution.