Site icon LinuxAndUbuntu

How To Compile C, C++, And Java Using Terminal On Linux

Compile C C And Java Using Terminal On Linux

Compile C C And Java Using Terminal On Linux

€‹This topic might surprise you because occasionally programmers are occupied primarily on one language and then they move on to another programming language with changing times. But that doesn’t mean languages like C, C++, and Java differ from another when it comes to compiling the program and running it on the terminal, except only the name of the compiler tool. Otherwise, it is a different matter when it comes down to BASH, Python, etc.  

Target audience

€‹People programming just now or recently.

Pros and Cons

€‹The pro is usually novice programmers tend to write console based programs before moving on to GUI programming. And particularly, there comes a time when it is feasible to pass arguments in the main function. That’s where the terminal emulator beats Integrated Development Environment(s).

The con is developing software on terminal begins to be quite awkward on big projects. Then again if you’re not good at organizing large chunk of files, your peers later will have the headache of building your program.  

A word of advice

€‹Please, don’€™t just skip down to C++ or Java too early whatever your reason is. Read on from C because some basic steps are covered on it. That way you won’€t get lost afterward.  

How To Compile C?

#include
int main()
{
printf("Hello World!\n");
return(0);
} 

Save the above code as hello.c then open up your file manager program, right click and select Open Terminal Here.

€‹Save the above code as hello.c then open up your file manager program, right click and select Open Terminal Here.

gcc -o hello hello.c 

And now the details:€‹gcc is the program you are invoking to compile your hello program.

-o is the first argument that tells your compiler to produce the output file as €œhello€, the second argument following it and hello.c is the input file that will be processed by gcc program you invoked. Running the program will come later.

How to compile C++?

‹Here is a program in C++ implementing for loop – #include <<span style=”color: red;”>

#iostream> Ignore the red # sign
int main()
{
int i = 0;
for(; i < 5; ++i)
std::cout << "We love GNU!\n";
return(0);
} 

Save the above program as loveGNU.cpp in the same directory where you did your hello program in C and proceed below to compile it on terminal. g++ -o loveGNU loveGNU.cpp

g++ -o loveGNU loveGNU.cpp

The convention is the same as what€™’s taught above only that g++ is used as the compiler while compiling C++ programs. The name gives it away right?

Running the program will come later.  

How to compile Java?

Save the Java program below as HelloJava.java in the same directory.

public class HelloJava {
public static void main(String args[]) {
System.out.println("Hello Java! ");
}
}

Now to compile this program, there are two things you need to consider:

Do I want only the class file? Or do I want to make an executable binary file?

It’s usual habit for first time Java programmers to compile java program using javac so here it is.

javac HelloJava.java then run it this way – java HelloJava

java HelloJava

Otherwise, to create a binary executable file you’ll have to invoke gcj this way – gcj -o HelloJava HelloJava.java –main=HelloJava

gcj -o HelloJava HelloJava.java –main=HelloJava  

Running the above programs

€‹Sticking true to UNIX style you run your program this way –

./program-name 

Some of you might have some doubts here so what is ./ function? Assume you created a program called foo and maybe one day you execute it just typing foo on your terminal. The thing is the program might run but the output you see will ultimately surprise you. The program that ran was not the program you created but someone else’s. So that €˜./€™ tells your computer to look for an executable file only within the current directory, not prefixing it with €˜./€™ as was the case above tells your computer to look for it on the possible directories (/bin, /usr/bin/, /usr/local/bin/, etc) where your binary executable files are installed.

This will be how you run the above programs:

./hello
./loveGNU
./HelloJava

Conclusion

€‹Before waving y’all “good luck and have fun” if you are focusing entirely on some small rudimentary programs I suggest you also learn how to use nano or vi. That speeds up your work because you don’t have to keep switching multiple windows over and over again. Then as you flourish with the programming language knowledge, you better move on to rich IDEs like Eclipse, Codeblocks, etc. to create projects and large scale software. Well, that’s it and let me know what you think in the comments section below.

Exit mobile version