How to Compile a C or C++ Program in Linux

Spread the love

If you have just started using Linux Ubuntu on your personal computer, then this is normal that you are not acquainted with all the commands in Ubuntu. For a Windows user, Linux Ubuntu is not a similar kind of operating system because in Windows everything is almost click and run. But in Ubuntu you will not be able to do everything with the click. You will have to write command in the terminal and then will get what you want. There are various kinds of advantage of using Linux operating system for developing computer programs. Also, there are some source codes which are not possible to compile and build without Linux operating system.

If you want to write a C or C++ code in Linux, you do not have to install any additional software. You can just open one text editor named “gedit” and can start writing the C or C++ code. If you are writing C then you will have to save the file with .c extension and if you are writing for C++ then you will have to save the file with .cpp extension. To open the “gedit“, you can simply type in the  Linux terminal (shortcut keyboard stroke Ctrl + Altr + T):

$ gedit

In C or C++ you will have to compile the file at first. At first the .cpp or .c file gets converted to object file which is machine readable. Then it becomes .exe file which we can run. If you have a test.c or test.cpp file then the step to compile the file is below:

For C:

$ gcc -o test test.c

$ ./test

If your program is 100% correct then only after the first command, one executable file will get created and you will be able to run it with the second command. But if there is any error in the program, you will get error message in the terminal and you need to fix your program accordingly. The second command helps you to run the executable file.

For c++:

$ g++ -o test test.cpp

$ ./test

For C++ also, if your program is not correct or has some bug, you will get error message but if it is okay then the executable file will get created and you can run it by using the second command.

That’s it. You will be able to run your program! Pleaes make sure that you are in the right directory. To check the directory path, you can simply type $ pwd and it will show your current directory path. While compiling the file, if you get any error message, check for whether the file extension the you have put is right or not. Because your file extension should be .c or .cpp

If you still face any problem, you can tell me through comments and I will be more than happy to answer you.

Himadri Subrah Saha

Himadri is an ICT Professional who writes for his technology tips & tricks related blog TechnTechie. Though it is hard to balance time in between professional life and blogging, he still manages time to work for his own blog and writes almost regularly. The dashboard of this WordPress is the only place where he does not feel tired! Read my other blogs @ PetCare and Teleinfo

0 0 votes
Article Rating

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x