Run the compiler from the command line

The command to run the c++ compiler varies from operating system to operating system. The most common compilers are the GUN compiler and the Microsoft Visual Studio compiler. hello.cpp

#include <iostream>

int main(a) {
    std::cout << "Hello World!" << std::endl;
    return 0;
}
Copy the code

The gnu compiler

By default, the command to run the GUN compiler is g++.

centos: yum install gcc-c++ ubuntu: apt install g++

g++ -o hello hello.cpp
Copy the code

Microsoft compiler

The command to run the Microsoft compiler is cl.

When You install Visual Studio, you install command-line tools that integrate the environment variables you need.

Open a command line tool as an administrator and run the cl command :cl /EHsc hello.cpp.

Your_project_path >cl /EHsc hello.cpp Microsoft (R) C/C++ optimized compiler for x8619.16.27045Copyright (C) Microsoft Corporation. All rights reserved. hello.cpp Microsoft (R) Incremental Linker Version14.16.27045.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:hello.exe
hello.obj
Copy the code

/EHsc is the compiler option to turn on standard exception handling.

Not surprisingly, an executable (.exe) is generated in the current directory, along with a binary.obj file (not linked and therefore not executable)