Linux, OpenSource, Programming And Hacks

Oct 2, 2016

How to Write, Compile and Run a C/C++ Program in Fedora Terminal

Oct 2, 2016

In this tutorial we will see how to how to write, compile and run a C or C++ program in fedora terminal itself i.e writing, compiling and execution of the program will be done within the terminal itself.

For writing any program we need a console (terminal) based text editor. I 'll be using vim editor, there are many more like nano, jed, emacs...

1. Open up the terminal and install vim by entering the following commands.
dnf -y update
dnf -y install vim-enhanced

2. Install necessary development tools (if not installed already). This includes the compiler.
dnf group install 'Development Tools'

3. Create a new C/C++ program with this vim command.

for C program
vi example.c
for C++ program
vi example.cpp

4. Press the i key or the insert key to start writing the program. After you have completed the program save and exit vim by typing :wq

example programs:

5.Compile the program with the following gcc command. this will create a object file, a.out which will will execute in the next step.

for C program
cc example.c

for C++ program
g++ example.cpp

6. Finally, execute the a.out object file which is created automatically after compiling the program

Output of the above example program.

To open an existing file use the same command which is used to create a new file
vi example.c 

for more vim commands visit this link

No comments:

Post a Comment