Skip to content

Latest commit

 

History

History
 
 

Compiling Code

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Compiling Code

We've been compiling and executing code by clicking the Run button. There may be scenarios where we will want to compile code in the absence of an IDE (Integrated Development Environment - Visual Studio, Xcode), for example if we wanted to deploy code on a remote server, on a cloud server, or remote high performance computing cluster. In such a scenario, we can compile and run code from the command line (aka, terminal, shell). For the following example we will be using the following files which are in this directory:

  • main.cpp is the file that contains the int main() {} function.
  • factorial.cpp contains a supporting function factorial() called by main.cpp.
  • print.cpp also contains a supporting function print() called by main.cpp.
  • function.h is a header file that declares the supporting functions factorial() and print().

Compiling Code from the Command Line

We can access the shell directly in repl.it (which is a linux ubuntu terminal) and compile the code as follows:

clang++-7 -o main factorial.cpp main.cpp print.cpp
  • clang++-7 is the compiler (alternatively one may use g++).
  • -o main defines the name of the output executable.
  • factorial.cpp main.cpp print.cpp defines the input source files.

Executing Compiled Code from the Command Line

We will now find an executable that we can execute from the command line.

./main

Output

The resulting output should be:

Factorial of [7] is: 5040

References: