【Tools】Basic use of VS

0 Preface

  Recently, due to project practice, I have been using VS a lot, and I have encountered and solved many problems during the use. Therefore, I would like to make a summary based on the blog posts on some third-party library configurations previously recorded.

Note: Here we use VS2017 pro as a reference, other versions are similar.

1 Download, installation and basic use

1.1 Download and install

  It is recommended to download and install directly from the official website.

VS2017 official download link

After opening, you can see the following interface:

Insert image description here

Find the version of VS 2017 pro and click to download.

If you want to download other versions, you can change the search content above and search again:

Insert image description here

You should be logged in with a Microsoft account to enter this page.

After downloading, just double-click to install:

Insert image description here

In the interface for selecting installed components, there are some large development categories on the left, and some small components corresponding to each category on the right. You can choose according to your own needs. For example, since I only need to develop C++ here, I only checked one:

Insert image description here

The space required to install these components is shown in the lower right corner.

Then just wait patiently for the installation to complete:

Insert image description here
Enjoy!

1.2 Project creation

  After the installation is complete, the next step is to create the project and develop it.

  • Open: File->New->Project
    Insert image description here

  • Select project type
    Insert image description here

  After the project is created, pay attention to the file directory and folder directory in the software:

Insert image description here

Insert image description here

It can be found:

  • A solution can contain multiple projects, and this is also a common method for large-scale project development, that is, the project is divided into modules, and each module corresponds to a project.

  • If there is only one project under the solution, the solution name does not have to be the same as the project name.

  • Whether the compiled and run file is called Debug or Release depends on the property configuration in the solution.
    Insert image description here

  • Each project has header files and source files, and their locations are not in one-to-one correspondence with the locations of the actual folders. When you right-click to create a new file, you can select the path where the file is located.

1.3 Compile, run and debug

  • Compilation
    In VS, compilation is called "Generate". In the menu bar Generate:
    Insert image description here
    If there are multiple projects in the solution, you can also right-click a single project to generate:
    Insert image description here
  • Running and debugging
      For C++, running and debugging should be two different instructions, but in VS, running and debugging are just one button. If there is a breakpoint, it is debugging, that is, it will stop when it reaches the breakpoint, and then it can be run single-step. If there is no breakpoint, it is running. In addition, if it is not generated and run directly, the program will be generated once and then run. This is easy to understand.

      I won’t mention how to set breakpoints anymore. Here we introduce a breakpoint setting function, that is, the conditions and operations for adding breakpoints: Insert image description hereInsert image description here
    This allows you to set a specific loop to stop when debugging some loop code, improving debugging efficiency.

1.4 Interface and settings

  Here is a brief introduction to some common operations of the software.

  • When opening various windows
      and using the software, we often encounter the problem of "a window has been forked, how can I get it back?" In fact, it is very simple. The "View" in the menu bar contains all the windows that need to be recalled:Insert image description here

You don’t need to write it all down, just check it out when you need it.

  • Adjust the position of the window
      VS Each functional window can be freely adjusted according to your own needs. For example, when editing .cpp and .h files at the same time, you can place two side-by-side windows:

Insert image description here
This allows you to compare function declarations and definitions, which is very convenient.

It should be noted that when dragging and dropping a window, the position of the window is determined by the position of the mouse.

  • Set the shortcut key
      to open "Tools => Options", search for "keyboard" directly in the search box, and then search for "annotation" in the display command, you can see the selected content of the annotation and the uncommentation.
    Insert image description here
    When setting a shortcut key, first select the command, focus the cursor in the "Press shortcut key" input box, then press the keyboard shortcut key, then click "Assign", and finally click "OK" to complete the setting.

  • To set the font and color of the editor text
      , open "Tools => Options" and find "Font and Color", as shown in the figure below.
    Insert image description here
    It is recommended to set Fira Code, Consolas and other programming fonts.

2 Project attribute configuration [Key points]

  The previous introductions are all basic functions. In fact, the biggest advantage of VS lies in project attribute configuration, which greatly facilitates the development of third-party packages.

2.1 Open the project property configuration window

  Right click on the project, scroll to the bottom and find properties
Insert image description here

Or click "Debug==>xx Properties" in the menu bar

Insert image description here

You can open a property configuration window:
Insert image description here
The first thing to note is that the above "configuration" is consistent with the project configuration:Insert image description here

Generally speaking, you first set the configuration in the project, and then open the properties interface, and it will naturally be consistent with the project configuration. Remember not to change the configuration in the project casually. If you must change it, you must reconfigure it again.

This is the basic premise for starting other configurations. You must not get it right or wrong, otherwise the configuration will not be usable at all.

2.2 Static libraries and dynamic libraries

  Before understanding the VS project property configuration, let's briefly understand the basic principles of C++ compilation and operation.
  We all know that C/C++, as a compiled language, needs to go through the four steps of "Edit==>Compile==>Link==>Run". The linking stage is to link libraries other than the main program. There are two main types: static (link) libraries and dynamic (link) libraries .

  • Static library
      The commonly used suffix of static library on Windows is .lib, and the suffix on Linux system is .a. It is loaded at compile time to generate the target file, and has no dependency on the library at run time. Generally speaking, a static library will have a corresponding header file that tells the compiler what function interfaces are available in the .lib file.

  • Dynamic library
      The suffix of the dynamic library on Windows is .dll, and the suffix on Linux is .so. It is in the running stage, that is, the exe execution file has been generated. The execution of the exe needs to rely on the dll file.

  • The advantages, disadvantages and usage scenarios of the two
      can be seen from the above introduction. The static library needs to be loaded at compile time, which will cause the final executable program to be very large. However, after the executable file is generated, there is no longer the problem of library dependency. The dynamic library is linked after the target is generated, so that the execution program will be smaller, but the operation will have dependencies.
      However, in practical applications, dynamic libraries are mostly used. This is because if the dependent libraries are updated, using a static library requires recompiling the software, while if using a dynamic library, you only need to update the library, that is, the dll file, which is simpler and more efficient.

2.3 Include Directory&Library Directory&Dependencies&Working Directory

  With the support of the above basic theory, it will be very simple to understand the various directories in the project attribute configuration.

  Various directories can be set in the "VC++ Directory":
Insert image description here

But if you modify the include directory here, its impact is global, and if you only modify the additional include directory, it only affects the current project and has no impact on other projects. (The same applies to library directories)

Therefore, we generally do not set it here, but set additional directories:

  • Additional include directories
    Insert image description here

  • Additional library directory
    Insert image description here

  • additional dependencies
    Insert image description here

  When there are many third-party dependencies, it is recommended to create a new folder in the project directory to place header files (.h), static libraries (.lib), and dynamic libraries (.dll include) lib:bin
Insert image description here

Then relative paths can be used for configuration in the project property configuration, which can ensure the portability of the project . Note that the relative path here is relative to the project directory :
Insert image description here
if you are not sure, you can use the macro provided by VS:
Insert image description here

Attachment: How to view VS macros:

Insert image description here
Insert image description here
Insert image description here

  You can find that the macros here are actually system environment variables plus VS custom macros, such as the previous project path, etc. The usage method is the same, that is $(xxxx), but please note that if it is a macro definition of the path, it depends on whether there is one at the end \, which determines whether the subdirectory needs to be separated by a separator.

  The last step is the setting of the working directory. The so-called working directory is the working path when running and debugging in the software. Generally speaking, it should be set to the path that contains all dynamic libraries, that is, the dependencies required at runtime.
Insert image description here

The default setting here is $(ProjectDir)the project directory. Therefore, if there are few dynamic libraries, there is no need to create a separate bin folder and then modify the working directory. You can directly throw the dll file into the project directory.

One thing to note here: directly clicking on the generated executable program is different from clicking to run in VS. The main difference lies in the difference in the working directory. Therefore, if you need to click to run the executable program, you need to copy the dependencies to the path where the executable program is located. Next, please remember.

2.4 Add additional dependencies to the code

  Some people may have this question: Why do you need to specify the specific lib file after adding the path where the lib file is located? Doesn't the include directory also specify a path? In fact, this is understandable. Both the include directory and the library directory tell the compiler where to find the files needed for compilation. However, the code has specified which file to include ( #include "xxxx.h"), but the link library does not tell the specific one to link. Therefore, specific library files need to be specified.

  In addition, in addition to setting additional dependencies in the project properties configuration, you can also set the libraries that need to be linked in the code:

#pragma comment(lib,"Ws2_32.lib")

Attachment: #pragmaIt is said to be the most complex precompiled instruction. Here is an article that summarizes it relatively completely. You can refer to: Link

2.5 Configure project environment variables

  Some third-party libraries may require the configuration of environment variables, but if every library is configured, it will be troublesome and may exceed the length of the environment variables. Therefore, for some environment variables used by a specific project, you can set them in the project properties:
Insert image description here

Set as:

PATH=路径1;路径2;.....;$(PATH);    #这里要以;分割不同的路径

2.6 Modify attribute configuration file

  First open the property manager

Insert image description here

Pay attention to the distinction between the property window and the property manager. Some properties may not be in other windows, but in a first-level subdirectory. It is recommended to open both at the same time, as shown below.

Insert image description here

You can see that when you click a property file in the property manager, the property window below will display the path to the file. Obviously, the properties files that come here are global files. In order not to pollute the system settings, it is recommended that if you need to store properties files, you can create a new properties file yourself:

Insert image description here

Then double-click to open it and configure it according to your needs. Once configured, you can select "Add existing property sheet" if other projects require the same configuration. This achieves a "permanent configuration" in a certain sense

2.7 Other common settings

2.7.1 Setting the C++ standard

  The default C++ standard should be C++ 11, which can be set to a newer version according to your own needs:
Insert image description here

2.7.2 Set project type

  If the project needs to be used by other projects, such as needing to export a dll, then you can set it here:
Insert image description here

2.7.3 The execution of programs generated by VS does not depend on the VS environment

  Sometimes programs written in VS cannot run in an environment without VS installed, and a bunch of errors will be reported, and various dll files cannot be found, as shown in the following pictures:
Insert image description here
Insert image description here

Insert image description here
In fact, the problem is very simple, that is, if the project configuration is selected as Debug, just change it to Release.

3 plugins

  VS also has a good feature, that is, it supports adding plug-ins, which can provide some customized functions. Adding plugins is also very simple:
Insert image description here

You can then search in the search box for:

Insert image description here

In addition, you can also download it from Microsoft’s official plug-in store and install it locally:
https://marketplace.visualstudio.com/

Insert image description here

The following introduces several commonly used plug-ins, which will be continuously updated with subsequent use.

3.1 Format plug-in Astyle

Insert image description here
After the installation is complete, you can find the plug-in settings in the options:

Insert image description here

Including setting whether to use spaces or tabs for indentation, the number of indentation characters, shortcut keys, etc.

3.2 Rainbow bracket Viasfora

  If there are too many nested brackets, you may not know the corresponding relationship between the brackets. In order to solve this problem, you can install a plug-in that can set rainbow brackets - Viasfora, but unfortunately, VS2017pro cannot search for this plug-in in the plug-in. Even those downloaded from the official plug-in website cannot be installed:
Insert image description here
By clicking "View Installation Log" in the lower left corner, we can see that the VS version supported by this plug-in is [17.0, 18.0)(left closed and right open?):
Insert image description here

The corresponding version of VS2017pro is 15.9.58:
Insert image description here

Obviously, the problem is that the plug-in version is too new, so I found the GitHub website of the plug-in , downloaded an old version and installed it.

Insert image description here

4 others

  Here we mainly summarize some tips during use

4.1 Quickly open the folder where the file is located

  Right-click the file tab and select "Open folder"
Insert image description here

4.2 Select multiple lines with the mouse at the same time

  When similar statements appear in the code and all need to be replaced, in addition to using find and replace, you can also use the multi-cursor function that comes with VS to select multiple lines at the same time:
Please add image description
Note that here, hold down the Alt key and then drag the mouse.

4.3 Updating…

Guess you like

Origin blog.csdn.net/ZHOU_YONG915/article/details/135369477