C++ easily compiled in the command line.

08 Aug 2017

“Compiling C++ is easy!”, - my terminal said this morning. You can get yours to do the same in under 5 minutes as well after reading this blogpost.

C++ has the fame of being intricate and hard to start with for people coming from R, Python, JavaScript, Matlab, Matematica, SAS - you name it. Indeed, it is much more strict and less forgiving than all the languages listed above. Anyhow, the speed-ups that it offers are unprecedented and are worth going through the pain of learning. For now, let us concentrate on compiling our first program.

To begin with, we need to create the C++ code itself (see below). After navigating to the directory with the code, compile with

g++ -Wall first_compile.cc -o first_compile

where ‘first_compile.cc’ and ‘first_compile’ are the names of the input and output files, respectively. Now run the program with

./first_compile

Alternatively, we can create a shell script called ‘first_compile.sh’ and list both the compillation and execution commands in it (see below). Then it suffies to type

sh first_compile.sh

in your command line.

Here are the code, that gets you started, and the corresponding shell script:

Now we can compile away and develop some high performance routines in C++!

comments powered by Disqus