- This repository contains essential C++ programs for college students.
- I recommend compiling the C++ programs using
g++in GNU/Linux.
- Ubuntu:
sudo apt install build-essential - Fedora:
sudo dnf install g++ - Most distributions bundle
gccandg++with them by default, so a seperate installation isn't required.
- Ensure
g++is installed by runningg++ --versioncommand. - Open a terminal emulator of your choice.
- Navigate to the relevant directory using
cdcommand. - Optionally, use
touch <filename>.cppcommand to create a CPP file. - Enter
g++ filename.cppto compile the file. Any compilation/syntactical errors in the program will be displayed here. - After a successful compilating, an output binary file
a.outwill be created by default. - You can also provide your own custom output name by adding
-oflag at the end ofg++command. For example,g++ <filename>.cpp -o <filename>. Now the binary will be created in a custom filename. - To run the binary files just type
./a.outor./binarynamein the terminal.
-
You can write this program in any IDE or text editor of choice.
-
Example code:-
/*helloworld.cpp*/
#include<iostream> //header file
using namespace std; //for using cin,cout commands without std:: tags
int main()
{
cout<<"Hello World"<<endl;
}- To create for the output for file we need compile it using a compiler like GCC,etc.
- Open Terminal in Linux and
cdinto the file's directory. - Type
g++ helloworld.cpporc++ helloworld.cppand enter this will create ana.outbinary file. - For creating an output file with cutstom name use
g++ helloworld.cpp -o helloworld. - Now we can run the binaries using
./a.outor./helloworld - Output in the terminal would be:-
Hello World