How to Compile CPP File to EXE
Method 1 of 2:
Using Microsoft Visual Studio
- 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 that doesn't come with a compiler, see Using MinGW instead to learn how to set up MinGW, a GCC compiler for windows.
- Create a new project. If you haven't already done so, click the File menu, select New, and then select Project.[1] A dialog window will appear.
- Set up your project: The steps are a little different depending on the version you're using:
- 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.[2]
- 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.
- 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.[3]
- Click Build Solution on the menu. This compiles your program into the EXE format. The results of your build 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.
Method 2 of 2:
Using MinGW
- Download the Minimalist GNU for Windows (MinGW). If you don't already have a compiler, MinGW is a great version of GCC for Windows that can compile C++ code. Go to http://www.mingw.org in a web browser and then follow these instructions:
- Click Downloads in the left panel.
- Click the blue-and-white button with a Windows logo below your operating system.
- If the file doesn't download immediately, click Save to download it now.
- Run the downloaded installer file. To do this, double-click the downloaded file, click Install, and then click Continue. The default installation directory is C:MinGW—leave it this way to make things easy, as you'll need to enter this later. The app will now install. When the installation is finished, click Continue to launch it.
- If you install MinGW to a different folder, make note of which folder it is so you can properly set your environment variables.
- Install the GCC files in MinGW. The remaining installation steps will download the actual GCC compiler and associated files:
- Select Basic Setup in the left column.
- Select all of the options in the right column and mark them all for installation as prompted.
- Click the Installation menu and select Apply Changes.
- Click Apply to start the download. When the download is complete, click Close.
- Right-click the Start menu and select System. This opens the Settings window to the "About" screen.
- Scroll down and click System Info in the right panel. It's toward the bottom of the panel under "Related Settings."
- Click Advanced system settings in the left panel. This opens the System Properties dialog.
- Click the Environment Variables button. It's near the bottom of the "Advanced" tab, which opens by default.
- Add the MinGW binary path variable. This lets you easily run the compiler from anywhere on your PC. Here's how:[4]
- Double-click Path under "System Variables" in the bottom section.
- Click the New button at the top-right corner.
- Type C:MinGWbin into the blank and click OK.
- Click OK again and close all open windows.
- Right-click the Start button and click File Explorer. Your file browser will appear.
- Navigate to the folder that contains your CPP file(s). The location will vary. If you're using a Visual Studio Code (the free code editor from Microsoft), you can quickly find the location in the app by right-clicking your .cpp file in the left panel and clicking Reveal in Explorer. Other editors may have an Open in Explorer option as well.
- Hold the ⇧ Shift key as you click an empty area in the folder. A menu will expand.
- Select Open Command window here. If you use Windows PowerShell, you'll see Open PowerShell Window Here instead. This opens a command prompt in the same folder as your CPP code.
- Run the command to compile your program. Type g++ yourprogram.cpp (replace that name with the name of your actual CPP file) and press ↵ Enter to compile your 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.1 ★ | 61 Vote
You should read it
- How to fix the loss of the New menu in the right-click menu
- How to Change How Long Until a Mac Asks for Your Password
- 2 simple ways to remove SkyDrive Pro option in Windows 10 Right-click Menu
- How to Change the Screen Resolution on a Mac
- How to add 'Edit or Run with' to the right-click menu of PS1 files
- How to fix slow context menu in Windows 10's File Explorer
- How to delete and improve the right-click menu
- How to Make a New File in Windows
May be interested
- C ++ editor onlineonline cplusplus editor has a very fast execution time, detects errors related to the syntax as soon as you type the code with a red x right on the error code, color the code ... and run as soon as you code by clicking go to execute.
- What is ISO file?an iso file, often called an iso image, is a single file, replacing all cds, dvds or bds. the entire contents of the disc can be accurately copied in a single iso file.
- How to open and read the .DAT file?file creation programs must have an extension for the file. however, in some cases a program will not do so and simply provide the .dat extension for the file only. technically this doesn't hurt files, but opening the file is a bit more difficult because your windows computer won't know how to open the .dat file.
- What is file 000?a file with the 000 extension is most likely an indexing service data file used to store locations where the windows operating system can perform file searches.
- What is DMG file?the main reason that macos uses dmg files is to make sure the file is properly downloaded and not spoofed. dmg files undergo a 'checksum' to verify that the file is 100% intact.
- HTML editor onlineonline html editor supports html5, when you type the opening tag automatically generates a closed tag, can detect errors related to the syntax when you are editing with the square icon with the x mark right on the error code line.
- What is an XML file and how to open it?the file with the .xml extension is the extensible markup language file (xml). this is really just plain text files that use custom tags to describe the structure and other features of the document.
- What is a DLL file, and how does this file work?dll stands for dynamic link library. dll file contains instructions that other programs can call to do a certain task. basically, dlls are the same as exes, but these files are named differently during the linking process.
- What is a .tmp file? How to open .tmp file on Windows computer?the article provides information about what a tmp file is, how to open a tmp file, how to delete a tmp file and answer frequently asked questions about tmp files. nguyen humanity what is the generated tmp file? let's see it now!
- What are UDF files?a file with the udf extension is most likely a universal disk format file or an excel user-defined function file.