Error LNK2019 in Visual Studio C++ Projects Using LabWindows™/CVI™ Functions

Updated May 28, 2021

Reported In

Software

  • LabWindows/CVI Base
  • LabWindows/CVI Full
  • Microsoft Visual Studio

Other

Microsoft Visual Studio

Issue Details

I am trying to use LabWindows/CVI functions in my Visual Studio C++ project, but when I try to compile my code I receive linking errors similar to the following:

error LNK2019: unresolved external symbol

Solution

In order to compile LabWindows/CVI functions in a Visual Studio C++ program, you first have to build the functions into a dynamic-link library (.dll) with an import library, and then link the import library (.lib) to your C++ project. To build the functions into a DLL and resolve the link errors, complete the following steps: 
  1. If the instrument driver that includes the functions is not already loaded into LabWindows/CVI, you will need to load it by going to Instrument>>Load. In the dialog box that opens, navigate to the .fp file of interest and click Load.
  2. Open the Function Tree Editor by going to File>>Open>>Function Tree (*.fp). In the dialog box that opens, navigate to the .fp file of interest and click Load.
  3. Create a DLL project by going to Options>>Create DLL Project. Specify a path and name for your project and click Save. A message will pop up asking you if you want to load the DLL project now, similar to the dialog below. Click Yes.
  1. Before building the DLL, you will need to configure some of the build settings. First set the build target type by going to Build>>Target Type and making sure there is a checkmark next to Dynamic Link Library. Also set the project to release mode by going to Build>>Configuration and ensuring there is a checkmark next to Release.
  2. Go to Build>>Target Settings and change the following settings:
    1. Change the Run-time support option to Full run-time engine, as shown in the image below
  1. Change the type library settings by clicking Type Library  and unchecking Add type library resource to DLL.  If you do not uncheck this option, you may run into a type definition error when attempting to build, similar to Definitions for these types could not be found.

Then click OK to exit out of both windows.
  1. Build the DLL. In LabWindows/CVI 2012 or earlier, select Build>>Create Release Dynamic Link Library. In LabWindows/CVI 2013, select Build>>Build. This will create a dynamic-link library (.dll) and an import library (.lib) containing the LabWindows/CVI functions you are trying to use. We can now link the import library to the Visual Studio project.
  2. Open your Visual Studio C++ project, and go to Project>>Properties. In the Properties window, go to Configuration Properties>>C/C++>>General. Click the Additional Include Directories field. Then click the arrow that appears and select <Edit...>.
  1. In the dialog box that opens, click the New Line button, add the directory that contains the header file and click OK.
  1. Navigate to Configuration Properties>>Linker>>Input. Click the Additional Dependencies field and then click the arrow that appears and select <Edit...>
  1. In the dialog box that opens, add the path of the import library to the list of Additional Dependencies.
  1. Compile your Visual Studio project, and the linking errors should be resolved. If you are still seeing linking errors, it may be that your project is dependent upon more than one library of LabWindows/CVI functions. In this case, you will want to repeat these steps for the other libraries.

Additional Information

If the functions you are trying to use are already bundled into a DLL with an import library, you can skip steps 1 through 6.

If you still has a link error, it is most probably due to name mangling issue. The C++ language allows you to overide some functions (such as methods in Object Oriented Programming). To resolve some names conflits, the C++ compiler add name mangling in order to have a unique name indentifier.
So when you call a C library in C++ programm, the C++ compiler needs to know that it do not need to modify the name. If it is the case, the name created by the C++ compiler is different from what is in your C code, and explain the linkage error.
In order to solve it, you need to add the following code in the header file (.h) related to you .c file.
 
#ifdef __cplusplus
extern "C" {
#endif
    /* The functions you want to keep the same in C and C++ */
#ifdef __cplusplus
}
#endif