Learn the process of setting up VSCode for C/C++ development and debugging a Dynamic Link Library (DLL) that is called by LabVIEW or TestStand.
- Install Visual Studio Code
- Install the C/C++ Extension
Note: The C/C++ extension for Visual Studio Code is provided by Microsoft to enable cross-platform C and C++ development on Windows, Linux, and macOS. When you create a *.cpp file, the extension adds features such as syntax highlighting, smart completions, hovers (IntelliSense), and error checking.
- Setting up the VS Code Environment
- Creating the Code
- Building the DLL
- Attaching to LabVIEW
- Attaching to TestStand
- Troubleshooting
- Conclusion
- Additional Resources
In this tutorial, we will configure VS Code for Microsoft C++, though other options like GCC and Clang are available.
If you have Visual Studio installed, open the Visual Studio Installer and ensure that "Desktop development with C++" is selected. If not, you can check it and then click "Modify."
If you don't have Visual Studio, you can install "Desktop development with C++" only. From the Visual Studio Downloads page, locate "Tools for Visual Studio" under the "All Downloads" section and select the download for "Build Tools for Visual Studio 2022."
To use MSVC from a command line or VS Code, you must run it from a Developer Command Prompt for Visual Studio. Regular shells like PowerShell, Bash, or the Windows command prompt lack the necessary path environment variables.
To open the Developer Command Prompt for VS, start typing 'developer' in the Windows Start menu, and you should see it in the suggestions list. The exact name depends on your Visual Studio version. Select the appropriate item to open the prompt.
Note: You can utilize the C++ toolset from Visual Studio Build Tools alongside Visual Studio Code to compile, build, and verify C++ codebases as long as you have a valid Visual Studio license (Community, Pro, or Enterprise) actively used for C++ development.
You can test if you have the C++ compiler (cl.exe) correctly installed by typing 'cl'; you should see a copyright message displaying the version and basic usage description.
Note: If the Developer Command Prompt starts at the BuildTools location (not suitable for projects), navigate to your user folder (e.g., C:\users{your username}) before creating new projects.
Create a Project for a Simple DLL.
- From the Developer Command Prompt, create an empty folder named "projects" to store your VS Code projects. Inside it, create a subfolder called "simple_dll." Navigate into it and open VS Code by entering the command
code .
mkdir projects
cd projects
mkdir simple_dll
cd simple_dll
code .
Note: The
code .
command opens VS Code in the current working folder, becoming your "workspace."
-
In the File Explorer title bar, click the New File button and name the file
simple_dll.cpp
. -
Paste the following source code into
simple_dll.cpp
and save it. This code returns the sum of inputsa
andb
, both of integer type.
#include <iostream>
// The function that adds two integers
extern "C" __declspec(dllexport) int AddIntegers(int a, int b) {
return a + b;
}
-
At the workspace root, create two folders: "bin" and "build." These will be used as destinations for binaries in a few steps.
-
Create a folder named
.vscode
at the workspace root, and within it, create the following two files:
launch.json
- configuration file used to define how you want to launch and debug your code in Visual Studio Code.
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to DLL Client",
"type": "cppvsdbg",
"request": "attach",
"processId": "${command:pickProcess}",
"symbolSearchPath": "${workspaceFolder}\\bin",
"stopAtEntry": false,
"justMyCode": false,
"sourceFileMap": {
"/mnt/c": "C:/"
}
}
]
}
tasks.json
- configuration file used to define tasks that automate build processes, code execution, or other custom actions.
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: Build DLL",
"command": "cl.exe",
"args": [
"/LD",
"/EHsc",
"/Zi",
"/Fo${workspaceFolder}\\build\\",
"/Fe${workspaceFolder}\\bin\\Add.dll",
"${file}"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Now, let's use VS Code to build the C++ DLL.
- Select the
simple_dll.cpp
file. - Open the Command Palette (Ctrl+Shift+P), type "Tasks: Run Build Task," and press Enter.
- After the build process completes, you'll find the generated binaries in both the "bin" and "build" folders.
With our DLL built, we can now use the VS Code debugger and attach it to LabVIEW.
- Launch your 32-bit version of LabVIEW.
- Create a blank VI, add a Call Library Function Node, and configure it to call the
simple_dll.dll
we created. - Run the VI and observe the successful execution of the Add operation.
- Back in VS Code, click the "Run and Debug" button (Ctrl+Shift+X).
- Choose "Attach to DLL Client" from the dropdown menu and click the green play button.
- Select the "LabVIEW.exe" process from the list after filtering results by typing "LabVIEW." VS Code is now attached to LabVIEW.
- Set a breakpoint on line 5 of
simple_dll.cpp
.
Note: Hover over the line numbers to reveal breakpoints. Clicking once sets a breakpoint, clicking again removes it.
- In LabVIEW, run the VI; the code will break in VS Code at line five. Use VS Code's debugging tools to analyze the code.
- After analyzing, click "Continue" in the VS Code debugger, and observe the VI completing execution.
With the DLL built, we can attach the VS Code debugger to TestStand.
- Launch the active version of TestStand.
- Create a new sequence.
- Add an Action Step using the C/C++ DLL Adapter, and provide a meaningful name.
- In the step properties pane, specify the
simple_dll.dll
. - Click "Verify Prototype" and point to
simple_dll.cpp
.
Note: The Verify Prototype button checks for conflicts between source code and parameter information on the Module tab. Refer to Parsing Parameters from Source Code for how the adapter interprets parameter declarations.
- Fill the VALUE EXPRESSION column for Return Value,
a
, andb
. - Back in VS Code, click "Run and Debug" (Ctrl+Shift+X).
- Choose "Attach to DLL Client" from the dropdown and click the green play button.
- Select the "SeqEdit.exe" process by filtering results with "SeqEdit." VS Code is now attached to TestStand.
- Set a breakpoint on line 5 of
simple_dll.cpp
.
Note: Hover over line numbers to manage breakpoints.
- In TestStand, run the sequence (using Run MainSequence, for instance). The code will break
If there are common issues or challenges that readers might encounter, list them here along with possible solutions or workarounds.
-
Issue 1: Can't run VS Code from a Developer Command Prompt.
- Follow the steps of Running VS Code Outside a Developer Command Prompt.
In this tutorial, you learned how to setup VS Code to develope and debug a C++ DLL that can be called from LabVIEW or TestStand. This is the base knowledge you need to either support a customer who has problems with their DLLs, or even create example codes that can be properly tested for integrtion with LabvIEW or TestStand.
Feedback: Feel free to suggest improvements to this article.
Author: Felipe Flores, a Senior Technical Support Engineer at NI.
Last Updated: August 15th, 2023