Is it possible to link and external library from my project in CMake


I have a C project using National Instrument library (through visa.lib/dll and visa.h).
I am implementing unit test on it (googletest).
My unit test is testing a function that calls a NI visa function (viClose()).
It seems the way i include the external library is not correct.

  • I create the external library dependency with by adding my visa.lib through the add_library and set_target_properties
  • Then i add this library to the one created by the current file

I am trying to add a reference of this external library in my CMakeLists but it fails “error LNK2019: unresolved external symbol viClose referenced…”

HEre is an extract of my CMake:

set (Headers file.h )
set (Sources file.c )

create dependency with NI external library

add_library(nivisa STATIC IMPORTED)
set_target_properties(nivisa PROPERTIES
IMPORTED_LOCATION “C:\Program Files (x86)\National Instruments\Shared\CVI\ExtLib\msvc\visa.lib”
INTERFACE_INCLUDE_DIRECTORIES “C:\Program Files (x86)\National Instruments\Shared\CVI\Include”

#build the library
add_library(${PROJECT_NAME}Lib STATIC ${Sources} ${Headers})
#inlcude external lib
“C:\Program Files (x86)\National Instruments\Shared\CVI\ExtLib\msvc”)
target_link_libraries(${PROJECT_NAME}Lib “C:\Program Files (x86)\National Instruments\Shared\CVI\ExtLib\msvc\visa.lib”)

The windows link library (i.e. visa.lib) should be specified with property IMPORTED_IMPLIB.
IMPORTED_LOCATION is for the runtime one (i.e. visa.dll).
And because you have .lib/.dll, the imported library must be of type SHARED.
And to finish, specify paths with slashes (CMake standard for paths) rather than backslahses.

add_library(nivisa SHARED IMPORTED)
set_target_properties(nivisa PROPERTIES
IMPORTED_IMPLIB “C:/Program Files (x86)/National Instruments/Shared\CVI/ExtLib/msvc/visa.lib”
IMPORTED_LOCATION “C:/Program Files (x86)/National Instruments/Shared/CVI/ExtLib/msvc/visa.dll”
INTERFACE_INCLUDE_DIRECTORIES “C:/Program Files (x86)/National Instruments/Shared/CVI/Include”)