Remote C/C++ Debugging with VSCode #2 - VSCode Makefile Tools

This article is a continuation of the previous blog(https://tipspiggy.blogspot.com/2022/05/remote-cc-debugging-with-vscode-1.html).

 Traditionally, Makefile is mainly used for Linux C/C++ development.(In fact, Makefile and make can be used not only in C/C++ but also in various development language projects.)  The usual purpose for Makefile in C++ projects is to recompile and link necessary files based on the modifications done to dependencies. 

 Compared to development tools such as Visual Studio on Windows, the extension sln and vcxproj files are used. When developing C/C++ using Visual Studio on Windows, most of us do not look at the contents of sln (Visual Solution File) and vcxproj (Visual Studio Project File) files. The reason is that if you select the properties of a solution or project in Visual Studio IDE, you can easily see the contents of the file in the property window.

However, although the Linux system uses a desktop mode that supports a GUI, there are also many cases where a server mode that does not support a GUI is used. Therefore, many developers use a lot of Makefiles that can build C/C++ projects in the terminal.

In February 2021, Microsoft released the Makefile Tools extension that automatically generates Makefiles. We will use this tool to manage a Makefile file on a remote Linux computer and proceed with the build.


Installing the Makefile Tools extension

First, install the Makefile Tools extension from the VS Code Marketplace. Refer to the previous article "Remote C/C++ Debugging with VSCode #1 - Installation and simple debugging" and open the directory on the remote computer again.

Install the Makefile Tools extension on the remote Linux system.



The make command exists in /usr/bin in most Linux systems, and since /usr/bin is included in the path environment variable, it can be used without using an absolute path. If the path of the make command cannot be found, the path of the make file must be specified separately in the settings of "Makefile Tools".

And after pressing the setting button, select "Edit in settings.json" of Makefile Configuration in the Workspace tab. A new settings.json file is created. Saving this file creates a settings.json file in the .vscode directory along with the previously created launch.json file.

Once the extension is installed, it will activate when it finds a Makefile in your project’s root folder. In the previous article, I made only one hello.cpp file and then built it with the "make -g hello.cpp" command, so the Makefile does not exist yet.


Make a sample Makefile and "Makefile Tools" configuration

Create and save the following simple Makefile.

#
# A simple makefile for compiling a c++ project
#

TARGET=hello
GCC = g++
CFLAGS = -g -o $(TARGET)

RM = rm -rf

all: clean default 

default: 
	$(GCC) $(CFLAGS) hello.cpp

clean:
	$(RM) $(TARGET) *.o 


And run "Makefile: Configure" from the Command palette.


There will now be a Makefile button on the left slider bar.


Click the Makefile button on the slide bar to set the configuration as follows. 

I made three rules in the Makefile: default, clean, and all.  


Build target selects all or default. If you always want to rebuild, select all.


Now, if you press the build button at the top, the rules of the selected build target are executed.


Finally, set the Launch target.


If you open the settings.json file, the program start information is included as follows. Binary Args are argument values to be passed to the program.



Press the run button at the top, and the hello program executes in the terminal.




Debugging

Debugging is also possible by using the debug button at the top. If you set a breakpoint in the source code and press the debugger button in the picture above, the code execution stops at the breakpoint while entering the debugger environment as shown below.



Debugging passing arguments to the executable program

Let's modify hello.cpp a bit to pass arguments to the executable program like this.


#include <iostream>

int main(int argc, const char * argv[])
{
    int a = 1;
    int b = 2;
    int c = a + b;
    std::cout << c << std::endl;
    if(argc > 1){
        for(int x = 1; x < argc; x++){
            std::cout << "parameter: " << argv[x] << std::endl;
        }
    }
    
    return 0;
}

And add the following arguments to binaryArgs in settings.js.  Now, when the run button is pressed, it asks whether to apply the arguments or not. If you select the option to use the second argument, the arguments are passed to the executable program.


You can see that the hello program is executed with the arguments passed to the terminal below. 
Passing arguments like this can be used in debugging as well.



Wrapping up

By using the Makefile Tools extension of VSCode, you can efficiently perform tasks such as build, rebuild, and debugging using the Makefile of a remote system in VSCode.

댓글

이 블로그의 인기 게시물

VSCode - Lua programming and debugging

Remote C/C++ Debugging with VSCode #1 - Installation and simple debugging

Remote Python Debugging with VSCode