Cross-Compiling ANSI C NI-DAQmx Examples for Linux RT Using Microsoft Visual Studio Code

Updated Aug 28, 2023

Environment

Driver

  • NI-DAQmx

Operating System

  • LabVIEW Real-Time (NI Linux Real-Time)

Programming Language

  • C
  • C++

In order to deploy NI-DAQmx ANSI C code to a Linux Real-Time target, you need to cross-compile the C code to be compatible with the target's Linux RT OS. This document explains how to become familiar with cross-compiling for Linux RT in Microsoft Visual Studio Code, and how to apply that knowledge to cross-compile ANSI C NI-DAQmx examples.

  1. Before attempting to cross-compile ANSI C NI-DAQmx examples, it is highly recommended to familiarize yourself with cross-compiling for Linux RT by going through parts 1 through 4 in the cross-compiling guide in the NI Linux Real-Time user group. This will ensure you have the necessary software downloaded, familiarize you with the Visual Studio Code environment, and give you a template to use for cross-compiling ANSI C NI-DAQmx  projects for NI Linux RT. The template created in the aforementioned guide will be the base for the subsequent steps.
  2. Once you have completed the steps in the guide mentioned above, create a new directory to serve as the top-level directory for your NI-DAQmx project, and copy your template project into this new directory. Launch Visual Studio Code, and open this new folder with the File >> Open Folder option.
  3. Create a new source code file in the .src directory titled <Name of NI-DAQmx Example>.c and save the file - for this example, the ReadDigPort NI-DAQmx example will be used. These examples can be found at C:\Users\Public\Documents\National Instruments\NI-DAQ\Examples\DAQmx ANSI C on your computer if you installed NI-DAQmx C Support when installing the NI-DAQmx driver. Open the .c file for the example you are trying to cross-compile, and copy the contents of the example's .c file into the new .c file in the Visual Studio Code project. Save the file.
  1. We will need to make some edits to the template files before building and cross-compiling this code. Specifically, we will need to edit CMakeLists.txt and c_cpp_properties.json in the template in order to ensure the NI-DAQmx dependencies are properly linked.
    1. In c_cpp_properties.json, edit the "includePath" section so that it looks like this, which will allow us to resolve editor errors related to dependencies/linking:
"includePath": [
   "${workspaceFolder}/**",
   "C:/Program Files (x86)/National Instruments/NI-DAQ/DAQmx ANSI C Dev/include",
   "C:/Program Files (x86)/National Instruments/NI-DAQ/DAQmx ANSI C Dev/lib/msvc"
],
Also, be sure to edit the "program" field in launch.json so that it reflects the name of the project you're trying to build. For example, for ReadDigPort, the line would look like:
"program": "${workspaceFolder}/build/bin/ReadDigPort",
 
  1. In CMakeLists.txt, edit the section dictating the root paths for CMake Find:
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) 
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH) 
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
Also, edit the "project specific information" section, which will allow us to include the necessary NI-DAQmx dependencies during the build:
# project specific information

cmake_minimum_required(VERSION 3.19)
project(ReadDigPort)
set(EXECUTABLE_OUTPUT_PATH bin)
set(CMAKE_BUILD_TYPE Debug)
set(THREADS_PREFER_PTHREAD_FLAG ON)

set(HEADER_DIR "C:/Program\ Files\ (x86)/National\ Instruments/NI-DAQ/DAQmx\ ANSI\ C\ Dev/include")
set(DAQMXLIBPATH "C:/Program\ Files\ (x86)/National\ Instruments/Shared/ExternalCompilerSupport/C/lib64/gcc")
#find_library(DAQMXLIBPATH NAMES nidaqmx HNTS "C:/Program\ Files\ (x86)/National\ Instruments/Shared/ExternalCompilerSupport/C/lib64/gcc")
add_executable(ReadDigPort../src/ReadDigPort.c ${HEADER_DIR}/NIDAQmx.h)
target_include_directories(ReadDigPort PUBLIC ${HEADER_DIR})
target_link_libraries(ReadDigPort PUBLIC ${DAQMXLIBPATH}/libnidaqmx.so)
Note that there are two ways to include the necessary dependent libraries and header files: hard-coding the paths on your system, or using the find_ function in CMake to search for the files in your computer. Hard-coding the paths is generally easier, but makes the solution unique to a single system and less robust. In the above screenshot, hard-coding method is utilized, but an example of what code to detect the dependent NI-DAQmx library using the find_library() function could look like is commented out. More information on the find_ functions can be found in the CMake command documentation.
  1. Once the above edits have been completed, the project can be cross-compiled and deployed to your Linux RT Target. The procedure from this point on is identical to the process outlined in part 4 of the Linux RT cross-compiling guide. Return to this guide, open Part 4, and follow the steps starting at the Building subsection on page 4 with your ANSI C NI-DAQmx example project to build, deploy, and run your cross-compiled NI-DAQmx code.
 
 

Upon completing this guide, you will have successfully built, deployed, and ran an ANSI C NI-DAQmx example on a Linux RT Target by cross-compiling the code. You can use this same process to compile other examples, or to write your own NI-DAQmx C code to run on a Linux RT Target.