Project

General

Profile

Linux Application Development using Visual Studio Code

Goal

This page describes how to use Visual Studio Code to build and debug a sample hello world program on the AM57x target.

Some people prefer the browsing capabilities and other extensions available in Visual Studio Code over the Code Composer environment. If you are primarily doing Linux user-level development, it can be a good alternative. For DSP development and other processors on the AM57x, you will probably want to use Code Composer.

When using Code Composer, there were two systems involved. The AM57x target and a host system like Linux which was running Code Composer. When using Visual Studio Code, there may be 3 systems involved. The AM57x target, a host system like Linux with the cross-compilers (and source code), and a Windows (or Linux) system running Visual Studio Code. The Visual Studio Code will connect to the Linux host system to build and debug the program.

Assumptions

The following are assumed:

  • The Linux Development environment has been installed as described in Code_Composer_Installation. Code Composer itself is not required but the TI SDKs and support libraries are needed.
  • The multi-architecture version of gdb has been installed as described in Gdb_Multiarch_Installation.
  • The target has an IP address which can be reached from your development host. These notes will use 10.0.103.50 as an example address. Substitute your address as needed.

Creating the Program and makefile

On the Linux host system, enter the following commands to create a source code tree and makefile.

cd ~
mkdir hello_vsc
cd hello_vsc
cat << EOF >hello.cpp
/*
 * hello.cpp
 *
 */
#include <stdio.h>

int main (int argc, char**argv)
{
    printf ("Hello world from visual studio code\n");
    fflush (stdout);
}
EOF
mkdir Debug
cd Debug
cat << 'EOF' >makefile
CC_PATH := ~/ti-processor-sdk-linux-am57xx-evm-06.03.00.106/linux-devkit/sysroots/x86_64-arago-linux/usr/bin
CC_PREFIX := arm-linux-gnueabihf-

# Add inputs and outputs from these tool invocations to the build variables 
CPP_SRCS += ../hello.cpp 

OBJS += ./hello.o 

CPP_DEPS += ./hello.d 

# Each subdirectory must supply rules for building sources it contributes
%.o: ../%.cpp
        @echo 'Building file: $<'
        @echo 'Invoking: Cross G++ Compiler'
        $(CC_PATH)/$(CC_PREFIX)g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<" 
        @echo 'Finished building: $<'
        @echo ' '

# Add inputs and outputs from these tool invocations to the build variables 

# All Target
all: Hello

# Tool invocations
Hello: $(OBJS) $(USER_OBJS)
        @echo 'Building target: $@'
        @echo 'Invoking: Cross G++ Linker'
        $(CC_PATH)/$(CC_PREFIX)g++  -o "Hello" $(OBJS) $(USER_OBJS) $(LIBS)
        @echo 'Finished building target: $@'
        @echo ' '

# Other Targets
clean:
        -$(RM) $(CC_DEPS)$(CLA_DEPS)$(C++_DEPS)$(EXECUTABLES)$(OBJS)$(C_UPPER_DEPS)$(CXX_DEPS)$(INO_DEPS)$(PDE_DEPS)$(K_DEPS)$(C_DEPS)$(CPP_DEPS) Hello
        -@echo ' '

.PHONY: all clean dependents
EOF

In makefiles, it is important that instead of leading spaces, there should be leading tab characters. Do the following to change any leading spaces into tabs:

vi makefile
:set tabstop=4 shiftwidth=4 noexpandtab
:retab!
:w
:q

Start Visual Studio Code

This can be done on either a Linux or Windows machine. This example use a Windows PC mostly in order to help separate the system running Visual Studio Code from the Linux host system running the compilers and connected to the target.

On your Windows PC:

These steps will just need to be done once:

  • Install vscode https://code.visualstudio.com/download
  • Install extensions
    • Remote Development (Microsoft)
    • C/C++ Extension Pack (Microsoft)
    • Doxygen Documentation Generator (Christoph Schlosser)
    • Shellcheck (Timon Wong)
    • SVN (Chris Johnston)
    • Native Debug (WebFreak)
    • Vim (vscodevim) - Optional
  • Restart vscode
  • Configure vscode
    • Setup ssh remote host
      • ctrl-shift-p > Remote-SSH: Add New SSH Host...
        <username>@<hostname_or_host_ip_address>
        
      • Click >< symbol in bottom left and select "Connect to host..."
      • Select <hostname_or_host_ip_address>
        vscode should connect to the specified host and let you edit code and run commands via the terminal as if you were natively on the machine
        First time may take a minute or so to setup
    • Install extensions on remote
      ctrl-shift-p > Remote: Install Local Extensions in 'SSH: <hostname_or_host_ip_address>'.... Select all extensions available.

This step will need to be done each time you start Visual Studio Code:

  • Connect to the remote Linux host
    • ctrl-shift-p > Remote SSH: Connect to Host
    • Start a terminal. Terminal->New Terminal
    • Enter the commands:
      cd hello_vsc
      code .
      

Create Visual Studio Support Files

These steps are within Visual Studio Code.

  • Create the tasks.json file.
    • ctrl-shift-p: Tasks: Configure Task
      • Select Create tasks.json file from template
      • Select Others as the template to use and open a window for tasks.json
      • Change the contents to be the following:
        {
            // See https://go.microsoft.com/fwlink/?LinkId=733558
            // for the documentation about the tasks.json format
            "version": "2.0.0",
            "tasks": [
                {
                    "label": "build",
                    "type": "shell",
                    "command": "cd Debug; make",
                    "problemMatcher": ["$gcc"],
                    "group": {
                        "kind": "build",
                        "isDefault": true
                    }
                },
                {
                    "label": "clean",
                    "type": "shell",
                    "command": "cd Debug; make clean",
                    "problemMatcher": [],
                    "group": {
                        "kind": "build",
                        "isDefault": true
                    }
                }
            ]
        }
        
      • Save the contents. (hit ctrl-s)
  • Create a Debug launch configuration
    • ctrl-shift-p -> Debug: Add Configuration...
      • Select GDB to open a window for launch.json
      • Change the contents to be the following:
        {
            // Use IntelliSense to learn about possible attributes.
            // Hover to view descriptions of existing attributes.
            // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
            "version": "0.2.0",
            "configurations": [
                {
                    "type": "gdb",
                    "request": "attach",
                    "name": "Attach to gdbserver",
                    "executable": "Debug/Hello",
                    "target": "10.0.103.50:10000",
                    "stopAtEntry": true,
                    "remote": true,
                    "cwd": "${workspaceRoot}", 
                    "valuesFormatting": "prettyPrinters",
                    "gdbpath": "/usr/bin/gdb-multiarch" 
                },
        
            ]
        }
        
      • Note that you will need to replace 10.0.103.50 with the ip address of your target. Note also that the :10000 is a port number and will need to match what is given to gdbserver on the target.
      • Save the file (ctrl-s)

Building the Program

These steps are within Visual Studio Code.

  • Start the build of the program. ctrl-shift-b
    • Select the target of the build. (choices are build or clean)
      • Note that other targets could be added. For instance, you may need a build for debug and build for release.

Running the Program on the Target

These steps are within Visual Studio Code.

  • Open a new terminal window in Visual Studio Code. Terminal->New Terminal
  • Execute the following commands
    cd Debug
    rsync Hello root@10.0.103.50:
    ssh root@10.0.103.50 ./Hello
    
    • You should see the output: Hello world from visual studio code

Debugging the Program

These steps are within Visual Studio Code.

  • Build the program
  • Copy the program to the target.
    cd Debug
    rsync Hello root@10.0.103.50:
    
  • Start gdbserver on the target.
    ssh root@10.0.103.50 gdbserver :10000 ./Hello
    
  • You should see output from the target like the following:
    Process ./Hello created; pid = 2101
    Listening on port 10000
    
  • Start Debugging
    • Method 1: ctrl-shift-p -> Debug: Start Debugging
    • Method 2: Select the debug icon on the left side of Visual Studio, then click on the "Attach to gdbserver" button at the top leftish part of the window.
  • This should result in showing hello.cpp with an arrow to the left of the printf line.
  • There should be a tool bar with icons for continue, step over, step in, step out, step back, reverse, restart, and disconnect.
  • Press the step over icon to step to the next line.
  • Press step over again to get past the fflush line.
    • On the terminal where gdbserver was started, you should see the output: Hello world from visual studio code
  • When you press continue, you will be offered a Pause icon instead of the continue icon. Try not to press disconnect unless the target is already stopped. If you try to disconnect while the target is running, it will try to disconnect but gdbserver will be confused and may restart without disconnecting and stopping.
  • Breakpoints can be set by clicking in the gutter next to the line numbers of the source file. A dot will appear when the breakpoint is successfully planted.
  • Press the disconnect icon to terminate the debug session.

Conclusion

A sample program has been built and debugged using Visual Studio Code. The program was makefile based. You could use something like autoconf and automake to help automate the generation of makefiles for a more complicated project. In this simple example, we just created the makefile by hand.

Go to top
Add picture from clipboard (Maximum size: 1 GB)