Debugging C + + projects with vs code compilation in CentOS 7

Source: Internet
Author: User

1. Installing Vscode

See Vscode official link https://code.visualstudio.com/docs/setup/linux#_rhel-fedora-and-centos-based-distributions

Download the Yum source First :

sudo rpm--import https://packages.microsoft.com/keys/microsoft.ascsudosh  'echo-e ' [code]\nname=visual Studio code\nbaseurl=https://packages.microsoft.com/yumrepos/ Vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc ">/etc/yum.repos.d/ Vscode.repo'

Installing Vscode

Yum check-update    #更新yum源 yuminstall code   #安装VSCode

2. Installing GCC

3. C + + compilation process

Suppose we have the following code hello.cc need to compile

#include <iostream>usingnamespace  std; int Main () {     "Hello, VS code! " << Endl;      return 0 ; }

The GCC compiler builds the application in two steps, which are compiled and linked. Where the result of the build is an. o file, the link generates an executable program or a static/dynamic library file, in Linux for. A,. SA,. La suffix files, executables in Linux can have no suffix, if not specifically specified, the default is a.out.

3.1 compiling hello.cc

g++-C hello.cc

The output is an hello.o file, which is the intermediate file generated by the compilation process. - C means compiling only, not linking .

3.2 Link HELLO.O build hello.out

g++-O-Hello.  out HELLO.O

Where-o indicates the name of the generated target file, if not specified, the default file name is A.out, generated, the destination file can have no suffix, which means the following command is also correct

g++-O Hello hello.o

Of course, if the 1th, 2 steps can be combined, execute the command directly

g++-O-Hello.  out Hello.cpp

3.3 Running Hello.out

./hello.out

The output is as follows:

Hello, VS code!

4. Build the Project

4.1 Install make

In Linux, to build the project to use make, first confirm that make is installed, enter the following command in the console:

 Make-V

If make is already installed, the version information will be output

3.82 for x86_64-redhat-linux-free  software Foundation, Inc.license GPLv33 or later //gnu.org/licenses/gpl.html>free   free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

Otherwise, you do not have make, to install do, use the following command:

Yum Install CMake

4.2 Preparing the Build script

Under current project root directory, enter

VI Makefile

Enter the following in the makefile:

1 hello:hello.o   2         g++ hello.o-o Hello             #按照makefile syntax, the front is not a space, but the TAB key, otherwise the build will fail 3 Hello.o:hello.  CC  4         g++-c-g-o hello.o hello.  CC    #按照makefile syntax, the front is not a space, but the TAB key, otherwise the build will fail 56         RM -F *.o                        #按照makefile syntax, the front is not a space, but the TAB key, otherwise the build will fail

Input: Wq save exit.

Explain the grammar of makefile,

Target ...: Prerequisites. Command    #注意前面是tab, not a space

Target is a destination file, which can be an object, an executable file, or a tag;

Prerequisites is the file or target required to generate the target;

command is what the make needs to execute (any shell command).

It's just that target, one or more targets, relies on the files in the prerequisites list, and its execution rules are defined in the command . If the file in the prerequisites list is newer than target, the command is executed or skipped. This is the rationale for the entire make process.

Note the-G parameter in line 3rd, in the process of generating the hello.o file, the g++ command in the- G indicates that the generated file is debug, and without-G, the breakpoint is not available for debugging.

By default, you only need to enter make, the following behavior occurs:

A. Make to find a file named Makefile or makefile in the current directory;

B. If found, it will look for the first target in the file, such as the build in the above file, and as the ultimate target file;

C. If the file for the first target does not exist, or its dependent. o File modification time is newer than the target file, the command will be executed immediately to generate the target file;

D. If the. o file on which the first target is dependent does not exist, a dependency of. O is found in the makefile file, and if it is found then the COMMAND,.O dependency must be. h or. cpp, so make can generate an. o file.

E. Follow-up to step B to achieve the final goal

Test the implementation of makefile:

[Email protected] hello]#ls-l#view a list of files before execution, only two files hello.cc makefile
Total 8
-rw-rw-r--1 lenmom lenmom 174 June, 17:05 hello.cc
-rw-rw-r--1 lenmom lenmom-June 17:43 Makefile
[Email protected] hello]# Make#执行make
g++-c-g-o hello.o hello.cc
g++ Hello.o-o Hello
[Email protected] hello]#ls-l#查看make之后的文件列表, found more than two files of Hello and hello.o
Total 56
-rwxr-xr-x 1 root root 21128 June 17 20:27Hello
-rw-rw-r--1 lenmom lenmom 174 June, 17:05 hello.cc
-rw-r--r--1 root root 23896 June 17 20:27hello.o
-rw-rw-r--1 lenmom lenmom-June 17:43 Makefile
[Email protected] hello]#./hello #执行hello文件
Hello VS Code #hello的执行输出
[Email protected] hello]# Make Clean#执行make Clean Clear Intermediate file
Rm-f *.O
[Email protected] hello]#ls-l #查看执行clean之后的文件列表 and found out that hello.o was gone.
Total 32
-rwxr-xr-x 1 root root 21128 June 20:27 Hello
-rw-rw-r--1 lenmom lenmom 174 June, 17:05 hello.cc
-rw-rw-r--1 lenmom lenmom-June 17:43 Makefile

5. Vscode Commissioning

5.1 Installing GDB

Yum Install GDB

5.2 Creating Launch.json

mkdir ./. Vscodevi.  /.vscode/launch.json

Enter the following:

1 {2 //Use IntelliSense to learn about possible attributes.3 //Hover to view descriptions of existing attributes.4 //For more information, visit:https://go.microsoft.com/fwlink/?linkid=8303875 "version": "0.2.0",6 "Configurations": [7 8         {9 "name": "C + + Launch",Ten "type": "cppdbg", One " Request": "Launch", A "Program ": "${workspacefolder}/Hello", - "args": [], - "Stopatentry": false, the "CWD": "${workspacefolder}", - "Environment": [], - "Externalconsole": false, - " Mimode": "GdB", + " prelaunchtask": "Build", - "Setupcommands": [ +                 { A " description": "Enable pretty-printing for GdB", at "text": "-enable-pretty-printing", - "Ignorefailures": True -                 } -             ] -         } -     ] in}

The 12th line, which represents the name of the program being started, and the output file after build in this example is hello.

Line 19th, build represents the task to be done before starting the debugging, obviously the project should be compiled before debugging, that is, make the following makefile to produce the latest project output.

So we're going to create a JSON file for the build task, where the task name is build and the task is referenced by launch, which is the meaning of build in line 19th.

VI  ./.vscode/tasks.json

Enter the following:

{    //See https://go.microsoft.com/fwlink/? linkid=733558    //For the documentation about the Tasks.json format    "version": "2.0.0",    "reveal": ' Always ', c5/> "Tasks": [        {            ["-F", "makefile"],              "label": "Build",            "type": "Shell",            "Command": "make"        }    ]}

This task means executing make-f makefile in the shell command line.

Next Select the C + + Launch "name in the Launch.json file" in Vscode, and click the Debug button to debug the project.

Debugging C + + projects with vs code compilation in CentOS 7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.