C++ Environment Setup
Local Environment Setup
If you want to set up a C++ language environment, you need to ensure that your computer has the following two pieces of software available: a text editor and a C++ compiler.
Text Editor
This will be used to input your program. Text editors include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim/vi.
The names and versions of text editors may vary across different operating systems. For example, Notepad is commonly used on Windows operating systems, while vim/vi can be used on both Windows and Linux/UNIX operating systems.
Files created by editors are typically called source files and contain program source code. Source files for C++ programs usually have extensions like .cpp, .cp, or .c.
Before you start programming, make sure you have a text editor and enough experience to write a computer program, save it in a file, compile, and execute it.
C++ Compiler
The source code written in source files is human-readable. It needs to be "compiled" into machine language so that the CPU can execute the program according to the given instructions.
A C++ compiler is used to compile the source code into the final executable program.
Most C++ compilers do not care about the extension of the source file, but if you do not specify an extension, it will default to .cpp.
The most commonly used free compiler is the GNU C/C++ compiler. If you are using HP or Solaris, you can use the compiler available on your respective operating system.
The following sections will guide you on how to install the GNU C/C++ compiler on different operating systems. C/C++ is mentioned here primarily because the GNU gcc compiler is suitable for both C and C++ programming languages.
Installing the GNU C/C++ Compiler
Installation on UNIX/Linux
If you are using Linux or UNIX, check if GCC is installed on your system by using the following command on the command line:
$ g++ -v
If the GNU compiler is already installed on your computer, you will see a message like this:
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr .......
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)
If GCC is not installed, please follow the detailed instructions at http://gcc.gnu.org/install/ to install GCC.
Installation on Mac OS X
If you are using Mac OS X, the quickest way to get GCC is to download the Xcode development environment from Apple's website and follow the installation instructions. Once you have installed Xcode, you can use the GNU compiler.
Xcode is currently available for download from https://developer.apple.com/download and requires an Apple ID to log in.
Installation on Windows
To install GCC on Windows, you need to install MinGW. To install MinGW, visit the MinGW homepage at mingw-w64.org, go to the MinGW download page, and download the latest version of the MinGW installer, named in the format MinGW-<version>.exe.
When installing MinGW, you should at least install gcc-core, gcc-g++, binutils, and MinGW runtime, but you will typically install more other items.
Add the bin subdirectory of your MinGW installation to your PATH environment variable so that you can specify these tools by their simple names in the command line.
Once the installation is complete, you can run gcc, g++, ar, ranlib, dlltool, and other GNU tools from the Windows command line.
Compiling with Visual Studio (Graphical Interface)
Download and install Visual Studio Community 2015.
Open Visual Studio Community.
Click File -> New -> Project.
On the left list, select Templates -> Visual C++ -> Win32 Console Application, and set the project name to MyFirstProgram.
Click OK.
Click Next in the following window.
In the pop-up window, select the Empty project option and then click the Finish button.
Right-click the folder "Source File" and click Add --> New Item...:
Select C++ File and set the file name to main.cpp, then click Add:
Copy the following code into main.cpp:
#include <iostream> int main() { std::cout << "Hello World!\n"; return 0; }
The interface is shown as follows:
Click Debug -> Start Without Debugging (or press Ctrl + F5):
After completing the above steps, you can see the following output:
g++ Application Instructions
The g++ program is a special version of gcc that sets the default language to C++. It automatically uses the C++ standard library instead of the C standard library during linking. It is possible to compile and link C++ programs using gcc by following the naming conventions of the source code and specifying the corresponding library names, as shown in the example below:
$ gcc main.cpp -lstdc++ -o main
Below is a simple C++ program code saved in the file helloworld.cpp:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!" << endl;
return 0;
}
The simplest way to compile:
$ g++ helloworld.cpp
Since the executable file name is not specified in the command line, the compiler uses the default a.out. The program can be run like this:
$ ./a.out
Hello, world!
Usually, we use the -o
option to specify the executable file name. The following example generates an executable file named helloworld:
$ g++ helloworld.cpp -o helloworld
Execute helloworld:
$ ./helloworld
Hello, world!
For multiple C++ code files, such as tutorialpro1.cpp and tutorialpro2.cpp, the compilation command is as follows:
$ g++ tutorialpro1.cpp tutorialpro2.cpp -o tutorialpro
This generates an executable file named tutorialpro.
On some systems, g++ defaults to using C++98. We can specify using C++11 to compile the main.cpp file:
g++ -g -Wall -std=c++11 main.cpp
Common g++ Command Options
Option | Explanation |
---|---|
-ansi | Supports only ANSI standard C syntax. This option disables certain GNU C features, such as the asm or typeof keywords. |
-c | Compiles and generates object files only. |
-DMACRO | Defines the MACRO macro with the string "1". |
-DMACRO=DEFN | Defines the MACRO macro with the string "DEFN". |
-E | Runs only the C preprocessor. |
-g | Generates debugging information. The GNU debugger can use this information. |
-IDIRECTORY | Specifies additional header file search path DIRECTORY. |
-LDIRECTORY | Specifies additional library search path DIRECTORY. |
-lLIBRARY | Links and searches for the specified library LIBRARY. |
-m486 | Optimizes code for the 486. |
-o FILE | Generates the specified output file. Used when generating an executable file. |
-O0 | No optimization. |
-O or -O1 | Optimizes code generation. |
-O2 | Further optimization. |
-O3 | More optimization than -O2, including inline functions. |
-shared | Generates a shared object file. Typically used when building shared libraries. |
-static | Disables the use of shared linking. |
-UMACRO | Un |
C++ is a statically typed, compiled, general-purpose, case-sensitive, and irregular programming language that supports procedural, object-oriented, and generic programming. It is considered a middle-level language, combining features of both high-level and low-level languages. C++ is a compiled language; C++ code needs to be converted to machine code by a compiler for the CPU to execute. Typically, Linux/Unix systems can run C++ once the GNU GCC compiler is installed; Windows requires installing MinGW to get GCC; and MacOS can configure GCC via Xcode. Here, I introduce a simpler way: starting your C++ project quickly through the Cloud Studio platform.
Cloud Studio has provided an integrated C/C++ development environment, where we only need to select it to run our C/C++ projects. Usually, C++ source files use .cpp, .cp, or .c extensions. Here is a small example:
Step1: Visit the Tencent Cloud Developer Platform, and register/log in.
Step2: Select the
"C/C++"
runtime environment from the right-hand menu, and check if GCC is installed successfully and view its version and configuration information with the command:g++ -v
Step3: Create a new C++ code directory in the left code directory and write your C++ code.
Step4: Navigate to the C++ code directory in the terminal, and compile the code you just wrote with the command:
g++ hello.c -o hello.out
. The -o parameter specifies the filename of the machine code file for distinction, after which a hello.out machine code file is generated in the directory.Step5: Enter the command:
./hello.out
in the terminal to see the output of the program you wrote./*Code Example*/ #include <iostream> int main() { std::cout << "Hello, Universe! I'm running on Cloud Studio!\n"; return 0; }
Tips: From the terminal commands, it is evident that Cloud Studio integrates a development environment of Ubuntu16.04.1 + GCC5.4 + Clang3.5.2.
For any questions, refer to the Help Documentation.
Currently, CODING is hosting a "My Favorite Cloud Studio Plugin Contest" based on Cloud Studio workspaces. Visit the event website for more information.