Happy file C++ environment settings

Table of contents

C++ environment settings

Local environment settings

text editor

C++ compiler

Install GNU's C/C++ Compiler

Installation on UNIX/Linux

Installation on Mac OS X

Installation on Windows

Compile with Visual Studio (Graphical Interface)

g++ application note

g++ common command options

Run C++ program {#cs-cpp} in Cloud Studio


Local environment settings

If you want to set up the C++ locale, you need to make sure that the following two software are available on your computer, a text editor and a C++ compiler.

text editor

This will be used to enter your program. Text editors include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim/vi.

The name and version of the text editor may vary on different operating systems. For example, Notepad is commonly used on Windows operating systems, and vim/vi is available on both Windows and Linux/UNIX operating systems.

Files created with an editor are often called source files, and source files contain program source code. Source files for C++ programs typically use the extension .cpp, .cp, or .c.

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

C++ compiler

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

The C++ compiler is used to compile the source code into the final executable program.

Most C++ compilers don't care about source file extensions, but if you don't specify an extension, .cpp will be used by default.

The most common freely available compiler is GNU's C/C++ Compiler, and if you are using HP or Solaris, you can use the compiler on your respective operating system.

The following sections will guide you on how to install GNU's C/C++ compiler on different operating systems. C/C++ is mentioned here at the same time, mainly because GNU's gcc compiler is suitable for C and C++ programming languages.

Install GNU's C/C++ Compiler

Installation on UNIX/Linux

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


$ g++ -v

If the GNU compilers are already installed on your computer, the following message is 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, then follow   the detailed instructions on Installing GCC- GNU Project to install GCC.

Installation on Mac OS X

If you're 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 Xcode is installed, you can use the GNU compilers.

Xcode is currently available for download from  Sign In - Apple  and requires an Apple ID to log in.

Installation on Windows

In order to install GCC on Windows, you need to have MinGW installed. In order to install MinGW, please visit the homepage of MinGW  mingw-w64.org , enter the MinGW download page, and download the latest version of the MinGW installation program, named as MinGW-<version>.exe.

When installing MinGW, you'll at least install gcc-core, gcc-g++, binutils, and the MinGW runtime, but usually many more.

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

When the installation is complete, you can run gcc, g++, ar, ranlib, dlltool, and a few other GNU tools from the Windows command line.


Compile with Visual Studio (Graphical Interface)

1. Download and install  Visual Studio Community 2015 .

2. Open Visual Studio Community

3. Click File -> New -> Project

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

5. Click OK.

6. Click Next in the following window

7. After selecting the Empty project option in the pop-up window, click the Finish button:

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

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

10. Copy the following code to main.cpp:


#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
    return 0;
}

The interface is as follows:

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 note

The program g++ is a special version that sets gcc's default language to C++, and it automatically uses the C++ standard library instead of the C standard library when linking. By following the naming convention of the source code and specifying the name of the corresponding library, it is feasible to compile and link the C++ program with gcc, as shown in the following example:


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

Here is the code for a simple C++ program stored in the file helloworld.cpp:


#include <iostream>
using namespace std;
int main()
{
    cout << "Hello, world!" << endl;
    return 0;
}

The easiest way to compile:

$ g++ helloworld.cpp

Since the file name of the executable program is not specified on 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 file name of the executable program, the following example generates a helloworld executable file:


$ g++ helloworld.cpp -o helloworld

Execute helloworld:


$ ./helloworld
Hello, world!

If there are multiple C++ code files, such as kxdang1.cpp, kxdang2.cpp, the compilation command is as follows:


$ g++ kxdang1.cpp kxdang2.cpp -o kxdang

Generate a kxdang executable.

g++ Some systems use C++98 by default, we can specify to use C++11 to compile the main.cpp file:

g++ -g -Wall -std=c++11 main.cpp

g++ common command options

options explain
-ansi Only the C syntax of the ANSI standard is supported. This option disables certain features of GNU C, such as the asm or typeof keywords.
-c Only compile and generate object files.
-DMACRO Define the MACRO macro with the string "1".
-DMACRO=DEFN Defines the MACRO macro with the string "DEFN".
-E Only run the C precompiler.
-g Generate debug information. The GNU debugger can use this information.
-IDIRECTORY Specifies additional header file search paths DIRECTORY.
-LDIRECTORY Specifies an additional library search path DIRECTORY.
-lLIBRARY Search the specified function library LIBRARY when connecting.
-m486 Code optimized for 486.
-o FILE generates the specified output file. Used when generating executables.
-O0 No optimization processing is performed.
-O or -O1 to optimize generated code.
-O2 advanced optimization.
-O3 More optimized than -O2, including inline functions.
-shared Generate a shared object file. Usually used when building shared libraries.
-static The use of shared connections is prohibited.
- MACRO Undefine the MACRO macro.
-w No warning messages are generated.
-Wall Generate all warning messages.

Run C++ program {#cs-cpp} in Cloud Studio

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 that combines features of high-level and low-level languages. C++ is a compiled language. C++ code needs to be   converted into machine code by a compiler before it can be run by the CPU. Usually, Linux/Unix systems can run C++ as long as the GNU  GCC   compiler  is installed  ; under Windows, GCC needs  to be installed  by installing MinGW ; under  MacOS,  GCC can  be configured  through Xcode  . Here I introduce you an easier way:   quickly start your C++ project through the Cloud Studio platform.

Cloud Studio  has provided me with a built-in C/C++ development environment, we only need to select it to run our C/C++ project, usually C++ source files use .cpp, .cp or .c as the extension, here is a small example:

  • Step1: Visit the Tencent Cloud Developer Platform , and register/login to your account.
  • Step2: Select from the operating environment menu on the right: "C/C++" operating environment, through the command: g++ -v you can check whether GCC is installed successfully and the version and configuration information of GCC
  • Step3: Create a new c++ code directory in the left code directory to write your c++ code
  • step4: Enter the c++ code directory in the terminal, and enter the command: g++ hello.c -o hello.out compile the code we wrote just now. The -o parameter is used to specify the file name of the generated machine code file for easy distinction. After that, a hello.out machine code file is generated in the directory
  • step5: Enter the command in the terminal: ./hello.out you can see the output of the program we wrote
/*代码示例*/

#include <iostream>

int main()
{
    std::cout << "Hello, Universe! I'm running on Cloud Studio!\n";
    return 0;
}

Tips:  Entering commands from the terminal shows that  Cloud Studio  has integrated  the development environment of Ubuntu16.04.1 + GCC5.4 + Clang3.5.2 for us  :

If you have any questions, you can consult the help documentation

Now CODING is holding a [my favorite Cloud Studio plug-in selection contest] based on the Cloud Studio workspace. Enter the official website of the event: https://studio.qcloud.coding.net/campaign/favorite-plugins/index to learn more about the event.

Guess you like

Origin blog.csdn.net/2301_77125316/article/details/131504954