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

**URL:** https://discourse.cmake.org/t/is-it-possible-to-link-and-external-library-from-my-project-in-cmake/4912
**Category:** Usage
**Tags:** os:windows
**Created:** [January 26, 2022, 8:55pm UTC](https://discourse.cmake.org/t/is-it-possible-to-link-and-external-library-from-my-project-in-cmake/4912 "2022-01-26T20:55:08Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![SHOAR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@SHOAR](https://discourse.cmake.org/u/SHOAR)
#### Post date: [January 26, 2022, 8:55pm UTC](https://discourse.cmake.org/t/is-it-possible-to-link-and-external-library-from-my-project-in-cmake/4912/1 "2022-01-26T20:55:08Z")

</div>

Hello,

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  
target\_include\_directories(${PROJECT\_NAME}Lib  
PUBLIC ${CMAKE\_CURRENT\_SOURCE\_DIR}  
“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”)

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [January 26, 2022, 10:20pm UTC](https://discourse.cmake.org/t/is-it-possible-to-link-and-external-library-from-my-project-in-cmake/4912/2 "2022-01-26T22:20:10Z")

</div>

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.

```cmake
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”)

```
