"Undefined Reference" Error When Cross Compiling C/C++ Code That Calls NI Drivers for NI Linux Real-Time Targets

Updated Aug 21, 2023

Reported In

Operating System

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

Programming Language

  • C
  • C++

Issue Details

I am working on developing an application in Visual Studio Code for my NI Linux Real-Time (RT) controller, following the instructions listed in the NI Linux Real-Time Cross Compiling: Using the NI Linux Real-Time Cross Compile Toolchain with Visual Studio Code tutorial. My code calls different NI drivers, such as NI-SWITCH, to perform the required functionality. Even though I do not get any errors when compiling my main code I always get several Undefined Reference errors after trying to run the task that builds the binary with Ninja. All of them seem to be related to the functions contained in the NI drivers I am using in my program, such as the one shown in the image below:

FAILED: bin/switch_1
 
c:/build/23.0/x64/sysroots/x86_64-w64-mingw32/usr/bin/x86_64-nilrt-linux/../../libexec/x86_64-nilrt-linux/gcc/x86_64-nilrt-linux/10.3.0/real-ld.exe: bin/libswitch1.so: undefined reference to `niSwitch_close'
 
error undefined ref.png

Solution

This error is occurring because the binary is missing the shared library (SO) file of the NI driver you are calling in your code. To solve the issue, follow the steps below:
  1. Get the required SO file for your application and save it in the same folder of your Visual Studio Code project on the host PC. The SO files can be found in the \usr\lib\ directory in the RT controller after installing software on it, and you can retrieve them using WebDAV.
libniswitch.png
  1. Open your CMakeLists.txt file and use the target_link_libraries function to link your binary with the SO file you have retrieved from your RT controller.
include SO file.png
  1. Rerun the CMake and Ninja tasks. If you keep getting Undefined Reference errors pointing to other drivers, repeat steps 1 and 2. Sometimes the main SO library depends on auxiliary or secondary SO libraries that must be added as well to the CMakeLists.txt through the target_link_libraries function.