How to Compile C and C++ Programs (2024)

In this tutorial you will learn about the processes you need to go through in order to compile your C (or C++) programs. We are going to use the UNIX's popular gcc compiler. You will need to download it's Windows port i.e. MinGW (Minimalist GNU for Windows). You can download it's latest installer version by clicking here. While installing MinGW, make sure you tick both C Compiler and C++ Compiler options when it asks to select components.

Important: After installing MinGW, you will need to add it's bin directory path to the %PATH% environment variable. To do this you can right click on Computer (My Computer) icon and from Advanced tab click on "Environment Variables". Select PATH variable from the list and click on edit. Now you can append the MinGW's bin directory path at the end separated by a semicolon. For example, if you have installed MinGW in "C:\MinGW\" then your bin directory path will be "C:\MinGW\bin".

Creating the program (Editing source code)

You can edit the C or C plus plus program's source code using the FireTXT text editor. You can open FireTXT in new tab of FireCMD from New Tab sub-menu of the File menu. You can also use any other ordinary editor like Notepad.

Note that the filename must end with ".c" (for C program) or ".cpp" (for C++ program) extension, e.g. myprog.c or myprog.cpp. The program code must obey C or C++ syntax. Discussing the syntax is not in the scope of this tutorial but you can use the following hello world c++ program code for testing.

#include <iostream>using namespace std;int main(){ cout << "\nHello World.\nFireCMD shell rocks!" << endl; return 0;}

You can copy the code given above, paste it in FireTXT editor and save it as "helloworld.cpp".

Compiling

Now we have the source code ready for compilation. If you don't have the latest version of FireCMD then you can download it from here.

Before giving command for compilation, you may need to change your current working directory in FireCMD shell to the directory location where "helloworld.cpp" or any other source code file that you want to compile exists. For example, if your c or cpp file resides in "C:\" drive then you can change your directory giving the command cd C:\. If you are already in the directory where your source code file resides then you don't have to give any command to change directory. You can check your current working directory using the pwd command. Note that it is not compulsory to change directory. You can avoid changing directory but then you will need to specify the complete(absolute) path to your c or cpp file while giving the commands given below to compile your program.

Just give the following command in FireCMD shell to compile your program:

C:\MinGW\bin\g++ -o helloworld.exe helloworld.cpp

If there are no errors in your code, FireCMD will return you to a new prompt without any error messages. This means that your code has been compiled successfully into a separate executable file named "helloworld.exe".

Note that we are specifying the complete path to gcc compiler i.e. "C:\MinGW\bin\g++". To avoid giving the complete path each time you want to compile a program, you can add the MinGW's bin directory to the path variable of FireCMD. To do this you can give this command: path -a. This will open Browse for Folder dialog box from where you will be able to select the directory. For example, if you have installed MinGW in "C:\MinGW\" then the path will be "C:\MinGW\bin\". After you have added the directory to the FireCMD's path environment variable, you can simply give the following command to compile.

g++ -o helloworld.exe helloworld.cpp

Note: If you have older version of FireCMD (Build 10 or older) then you will need to prepend the commands given above with keyword "cmd:". For example: "cmd: g++ -o helloworld.exe helloworld.cpp".

Running the program

You can run the executable program by simply typing in "helloworld" or "helloworld.exe" at the prompt as shown in the image below.

How to Compile C and C++ Programs (1)


How to Compile C and C++ Programs (2024)

FAQs

Can you compile C and C++ code together? ›

The C++ language provides mechanisms for mixing code that is compiled by compatible C and C++ compilers in the same program. You can experience varying degrees of success as you port such code to different platforms and compilers.

Does C compile faster than C++? ›

If you compile identical code in C and in C++, you typically get identical machine code generated…the speed is 100% identical. So C++ can only be slower than C if you use the extra features it offers in ways that are inappropriate for high performance programming.

Can I use C and C++ in unity? ›

Unity is a cross-platform game engine that is primarily used to develop video games and simulations for computers, consoles, and mobile devices. Even though Unity uses C# for its scripts, we can use Native plugins to use C/C++ in our projects. The first thing we have to do is create a Dynamic-Link Library project.

How do you link a C++ program to C functions? ›

Just declare the C function extern "C" (in your C++ code) and call it (from your C or C++ code). For example: // C++ code. extern "C" void f(int); // one way.

What is the command to compile C code? ›

The Unix command for compiling C code is gcc.

Which is the hardest C or C++? ›

C is much easier to learn than C++ because there is so much less of it. After you know C++, it's often easier to write in it because it provides features not available in C. Honestly though, the things that make programming difficult have, in my experience, little to do with the language you're using.

Why is C so much harder than C++? ›

C gives direct control over memory and hardware. As a superset, C++ has the same control but with easier tools. If you are learning C, you'd be learning the hard way to do things but you'd also have more control over what you do later.

Is C more difficult than C++? ›

C does not have so many rules and restrictins as C++ it is not so difficult to learn it because it is more direct to code. On other hand the C++ is more powefull to use. The conclusion is C is easier to learn, C++ is easier to use.

Does C++ compile to EXE? ›

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.

Where can I write and run a C++ program? ›

An IDE (Integrated Development Environment) is used to edit AND compile the code. Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These are all free, and they can be used to both edit and debug C++ code. Note: Web-based IDE's can work as well, but functionality is limited.

Can you compile C with Visual Studio Code? ›

C/C++ support for Visual Studio Code is provided by a Microsoft C/C++ extension to enable cross-platform C and C++ development on Windows, Linux, and macOS.

Are all C programs valid C++ programs? ›

In the strict mathematical sense, C isn't a subset of C++. There are programs that are valid C but not valid C++ and even a few ways of writing code that has a different meaning in C and C++. However, C++ supports every programming technique supported by C95 (C90 plus an Amendment) and earlier.

Is C++ good for concurrent programming? ›

Concurrency is essential in C++ for building responsive and scalable applications. It enables tasks like handling user input while performing background computations or processing multiple network requests concurrently. Properly managed concurrency can significantly improve the performance of your C++ applications.

Top Articles
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 6051

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.