Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Environment Setup

C++ Environment Setup

If you want to set up a C++ development environment, you need to ensure the following two pieces of software are available on your computer: a text editor and a C++ compiler.

Text Editor

Files created through an editor are usually called source files, which contain the program's source code.

C++ program source files typically use the extensions .cpp, .cp, or .c.

Before you start programming, make sure you have a text editor and enough experience to write a computer program, then save it in a file, compile, and execute it.

More Related Tools

C++ Compiler

Source code written in source files is human-readable. It needs to be "compiled" into machine language so the CPU can execute the program according to given instructions.

A C++ compiler is used to compile source code into a final executable program.

Most C++ compilers do not care about the source file extension, but if you do not specify one, .cpp is used by default.

The most commonly used free compiler is the GNU C/C++ compiler; if you are using HP or Solaris, you can use the compiler on the respective operating system.

The following sections guide you on how to install the GNU C/C++ compiler on different operating systems. C/C++ is mentioned together here mainly because GNU's gcc compiler suits both the C and C++ programming languages.


Installation on UNIX/Linux

If you are using Linux or UNIX, use the following command on the command line to check whether GCC is installed on your system:



$ g++ -v

If the GNU compiler is already installed on your computer, the following message will be displayed:



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 instructions on https://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 install it following the instructions. Once Xcode is installed, you can use the GNU compiler.

Xcode is currently available from https://developer.apple.com/download , and requires an Apple ID to log in.


Installation on Windows

Cygwin

Cygwin is a package that simulates a Unix/Linux environment on the Windows operating system, allowing users to use Unix-like tools and applications on Windows.

Cygwin provides a set of DLLs (Dynamic Link Libraries) that act as a bridge between the Unix system call layer and the Windows kernel, enabling Unix programs to run on Windows.

Cygwin official site: https://www.cygwin.com/.

Download the installer from the official site:

After the download completes, double-click the downloaded file:

Then keep clicking Next:

Here you can add the NetEase open-source mirror or Aliyun mirror https://mirrors.aliyun.com/cygwin/:

After installation completes, an icon is generated on the desktop:

Double-click the icon to enter the command line interface; enter the command cygcheck -c cygwin to view the current Cygwin version information:

Next, we install the gcc/g++ compilation environment; enter the setup-x86_64.exe directory on the command line and execute:

setup-x86_64.exe -q -P wget -P gcc-g++ -P make -P diffutils -P libmpfr-devel -P libgmp-devel -P libmpc-devel

After installation, enter the Cygwin64 terminal and type the command gcc --version to view the version information.

MinGW-w64

To install GCC on Windows, you need to install MinGW-w64.

MinGW-w64 is an open-source project that provides a complete GCC toolchain for Windows, supporting compilation of both 32-bit and 64-bit Windows applications.

Visit the MinGW-w64 homepage mingw-w64.org, go to the MinGW download page https://www.mingw-w64.org/downloads/, and download the latest version of the MinGW-w64 installer.

You can also download from Github:https://github.com/niXman/mingw-builds-binaries/releases

The MinGW-w64 download details page contains many integrated packages of MinGW-w64 and specific tools:

We only install MinGW-w64, so just download MinGW-w64; click the "SourceForge" hyperlink in the red box to enter the MinGW-w64 download page on SourceForge.

Download MinGW-w64 from SourceForge:https://sourceforge.net/projects/mingw-w64/files/

Scroll down the page and download the installer:

This installation may encounter network connection errors, so we can directly download sjlj (stable, supporting both 64-bit and 32-bit):

After the download completes, unzip it; you will find g++.exe or gcc.exe in the bin directory:

When installing MinGW, you must at least install gcc-core, gcc-g++, binutils, and MinGW runtime, but usually more items are installed.

Add the bin subdirectory of your installed MinGW to your PATH environment variable, so you can specify these tools by simple names on the command line.

When 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)

1. Download and install Visual Studio download.

2. Open Visual Studio Community

3. Click File -> New -> Project

4. In the left list, select Templates -> Visual C++ -> Win32 Console Application, and set the project name to MyFirstProgram.

5. Click OK.

6. In the following window, click Next

7. In the popup window, select the Empty project option, then click the Finish button:

8. Right-click the Source File folder and click Add --> New Item... :

9. Select C++ File, then set the file name to main.cpp, then click Add:

10. Copy the following code into main.cpp:



#include 



int main()

{

    std::cout << "Hello World!\n";

    return 0;

}

The interface is shown below:

11. Click Debug -> Start Without Debugging on the menu (or press Ctrl + F5):

12. After completing the above operations, you can see the following output:


g++ Application Notes

The g++ program is a special version of gcc that sets the default language to C++; at link time it automatically uses the C++ standard library instead of the C standard library. By following source code naming conventions and specifying the corresponding library name, it is possible to use gcc to compile and link C++ programs, as shown in the example below:



$ gcc main.cpp -lstdc++ -o main

Below is the code of a simple C++ program saved in the file helloworld.cpp:



#include 

using namespace std;

int main()

{

    cout << "Hello, world!" << endl;

    return 0;

}

The simplest compilation method:

$ g++ helloworld.cpp

Since no executable filename is specified on the command line, the compiler uses the default a.out. The program can be run as follows:



$ ./a.out

Hello, world!

We usually use the -o option to specify the executable filename; the following example generates an executable named helloworld:



$ g++ helloworld.cpp -o helloworld

Run helloworld:



$ ./helloworld

Hello, world!

If there are multiple C++ code files, such as runoob1.cpp and runoob2.cpp, the compilation command is:



$ g++ runoob1.cpp runoob2.cpp -o runoob

This generates an executable file named runoob.

On some systems g++ defaults to C++98; we can specify 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 features of GNU C, such as the asm or typeof keywords.
-c Only compile and generate the object file.
-DMACRO Define the MACRO macro as the string "1".
-DMACRO=DEFN Define the MACRO macro as the string "DEFN".
-E Only run the C preprocessor.
-g Generate debugging information. The GNU debugger can use this information.
-IDIRECTORY Specify an additional header file search path DIRECTORY.
-LDIRECTORY Specify an additional library search path DIRECTORY.
-lLIBRARY Search the specified library LIBRARY at link time.
-m486 Optimize code for the 486.
-o Generate the specified output FILE. Used when generating an executable file.
-O0 No optimization.
-O or -O1 optimize the generated code.
-O2 Further optimization.
-O3 Optimize further beyond -O2, including inline functions.
-shared Generate a shared object file. Usually used when building shared libraries.
-static Disable shared linking.
-UMACRO Undefine the MACRO macro.
-w Do not generate any warning messages.
-Wall Generate all warning messages.